剑指 Offer 46. 把数字翻译成字符串

public int translateNum(int num) {
        //动态规划
        if(num<10) return 1;
        String s = String.valueOf(num);
        int len = s.length();
        int[] dp = new int[len+1];
        dp[0] = 1;
        dp[1] = 1;
        int i = 2;
        while(i<=len){
            int x = Integer.parseInt(s.substring(i-2,i));
            if(x>=10 && x<=25){
                dp[i] = dp[i-1]+dp[i-2];
                i++;
            }else{
                dp[i] = dp[i-1];
                i++;
            }
        }
        return dp[len];
    }

 

posted @ 2020-08-25 11:33  欣姐姐  阅读(116)  评论(0编辑  收藏  举报