题解 | #把数字翻译成字符串#

把数字翻译成字符串

http://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668

import java.util.*;


public class Solution {
    /**
     * 解码
     * @param nums string字符串 数字串
     * @return int整型
     */
    public int solve (String nums) {
        int[] dp = new int[nums.length()+1];
        if(nums.charAt(0) == '0') return 0;
        dp[0] = 1;
        dp[1] = 1;
        for(int i = 2; i <= nums.length(); i++){
            if(nums.charAt(i-1) == '0'){
                int temp = Integer.parseInt(nums.substring(i-2,i));
                if(temp > 0 && temp <= 26){
                    dp[i] = dp[i-2];
                }else{
                    return 0;
                }
            }else{
                dp[i] = dp[i-1];
                int temp = Integer.parseInt(nums.substring(i-2,i));
                if(temp <= 26 && temp >10){
                    dp[i] += dp[i-2];
                }
            }
        }
        return dp[nums.length()];
    }
}
全部评论

相关推荐

头像
03-18 09:09
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务