题干解读:要求将输入的字符串中的字符按字母表向前移动n位
解题思路:先写一个移动字符的函数decord,在该函数内对n向26取模,得到t,t为等效移动长度.
移动到ASCLL表的非字符部分,先进行处理,等效到字符表中,
最后遍历字符串,调用移动字符的函数decord即可.
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 进行凯撒解密
* @param password string字符串 旺仔哥哥的密码
* @param n int整型 每个字符加密过程中错位的次数
* @return string字符串
*/
string decodeWangzai(string password, int n) {
for(int i=0;i<password.length();i++){
password[i] = decode(password[i], n);
}
return password;
// write code here
}
char decode(char c,int n){
int t=n%26;
if(c-t<'a'){
return 'z'- (t-1 - (c-'a'));
}else{
return c-t;
}
}
};

京公网安备 11010502036488号