题解 | #把数字翻译成字符串#
把数字翻译成字符串
https://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 解码 * @param nums string字符串 数字串 * @return int整型 */ int solve(string nums) { // write code here vector<int> dp(nums.size() + 1, 1); if (nums.size() == 0) { return 0; } if (nums.size() == 1) { if (nums[0] == '0') return 0; return 1; } for (int i = 2; i <= nums.size(); i++) { string temp; temp.push_back(nums[i - 2]); temp.push_back(nums[i - 1]); if (temp[1] == '0') { if (temp[0] == '0') { return 0; } if (stoi(temp) <= 26) { dp[i] = dp[i - 2]; } else { return 0; } } else { if (temp[0] != '0' && stoi(temp) <= 26 && stoi(temp) >= 1) { dp[i] = dp[i - 1] + dp[i - 2]; } else { dp[i] = dp[i - 1]; } } } return dp[nums.size()]; } };