题解 | #把数字翻译成字符串#
把数字翻译成字符串
http://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668
* 解码
* @param nums string字符串 数字串
* @return int整型
*/
function solve( nums ) {
// write code here
let dp=[]
if(nums=='0') return 0
dp[0]=1,dp[1]=1
let tmpnum=0
for(let i=2;i<nums.length+1;i++){
tmpnum=Number(nums[i-2]+nums[i-1])
if(tmpnum<=26&& tmpnum>20||tmpnum<20&&tmpnum>10)
dp[i] = dp[i-1] + dp[i-2];
else if(nums[i-1]=='0'&&nums[i-2] > '2' )
return 0;
else
dp[i]=dp[i-1]
}
return dp[nums.length]
}
module.exports = {
solve : solve
};