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

把数字翻译成字符串

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()];
    }
}
全部评论

相关推荐

06-07 21:26
江南大学 C++
话不多说,直接上时间线和图片1.2024年10月底发offer,并签三方2.2025年5月底公司违约
从零开始的转码生活:希望所有签了三方但直接违约的公司都倒闭!都倒闭!都倒闭!
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
04-25 10:45
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务