剑指OFFER----面试题46. 把数字翻译成字符串
链接:https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof/submissions/
代码
class Solution {
public:
int translateNum(int num) {
int n = 0;
string s = "";
while (num) {
s += to_string(num % 10);
num /= 10;
n++;
}
reverse(s.begin(), s.end());
vector<int> f(n + 1);
f[0] = 1;
for (int i = 1; i <= n; ++i) {
f[i] = f[i - 1];
int t = (s[i - 2]- '0') * 10 + s[i - 1] - '0';
if (t >= 10 && t <= 25) f[i] += f[i - 2];
}
return f[n];
}
};