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