题解 | #把数字翻译成字符串#
把数字翻译成字符串
https://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668
class Solution { public: /** * 解码 * @param nums string字符串 数字串 * @return int整型 */ int solve(string nums) { vector<int> ans(nums.size()+1,1); for(int i=nums.size()-1;i>=0;i--){ if(nums[i]=='0'){ ans[i]=0; } else if(nums[i]=='1'){ ans[i]=ans[i+1]+ans[i+2]; } else if(nums[i]=='2'){ if(nums.size()-1==i){ ans[i]=1; } else if(nums[i+1]>='0'&&nums[i+1]<='6'){ ans[i]=ans[i+1]+ans[i+2]; } else{ ans[i]=ans[i+1]; } } else{ ans[i]=ans[i+1]; } } return ans[0]; } };