题目地址:
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/
给定一个长
n
n
n的加密过的字符串,其原文是个只含小写英文字母的串,加密方式是,将'a'
变为'1'
,'b'
变为'2'
,等等,如果变的数字是两位数,则再在后面跟一个'#'
做标记。求其原文。
代码如下:
class Solution {
public:
string freqAlphabets(string s) {
string res;
for (int i = s.size() - 1; i >= 0; i--) {
if (s[i] == '#') {
int t = s[i - 1] - '0' + 10 * (s[i - 2] - '0');
res += 'a' + t - 1;
i -= 2;
} else if (s[i] == '0') {
int t = 10 * (s[i - 1] - '0');
res += 'a' + t - 1;
i--;
} else
res += 'a' + s[i] - '0' - 1;
}
reverse(res.begin(), res.end());
return res;
}
};
时空复杂度 O ( n ) O(n) O(n)。