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

把数字翻译成字符串

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

相关推荐

能干的三文鱼刷了100道题:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
牛客383479252号:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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