首页 > 动态规划 > 线性DP > 数字转字母的编码方式的个数-[动态规划]
2014
11-05

数字转字母的编码方式的个数-[动态规划]

问题

假设1到26之间的26个数据,可以编码为对应的’A'-’B',对于一个给定的数字字符串,可以有多少种编码方式?假定输入都是合法的(不会有”30″)。

举例:

输入:  digits[] = "121"
输出: 3
// "ABA", "AU", "LA"

输入: digits[] = "1234"
输出: 3
// "ABCD", "LCD", "AWD"

 分析

很容易想到递归的方法,化解为小的子问题,从最后一个数字开始:1) 最后一个数字是非零的,递归剩下的 (n-1) 个数字。

2) 最后两个数字在小于27,则把最后两个数字结合,递归剩下的(n-2)个数字。

public class CountDecoding {

    static int countDecoding(String digits,int n){
        if(n < 2) return 1;
        int count = 0;
        char[] chars = digits.toCharArray();
        //如果是0的话, 0肯定是和前面的一个数字组合成10,20
        if (chars[n-1] > '0')
            count =  countDecoding(digits, n-1);

        //如果当前的数字可以和前面的数字组合成一个字母
        if (chars[n-2] < '2' || (chars[n-2] == '2' && chars[n-1] < '7') )
            count +=  countDecoding(digits, n-2);
        return count;
    }

    public static void main(String args[]){
        String test = "1234";
        System.out.println(countDecoding(test, test.length()));
    }

}

上面的复杂度太高,有很多重复的计算。可以发现,其实和斐波那契数列数列是比较类似的。可以优化为动态规划的解法。

public class CountDecoding {
    static int countDecodingDp(String digits){
        if(digits.length() < 2) return 1;
        char[] chars = digits.toCharArray();
        int dp[] = new int[chars.length+1];
        dp[1] = 1;
        dp[0] = 1;
        for(int i=2; i<=chars.length; i++){
            //如果是0的话, 0肯定是和前面的一个数字组合成10,20
            if(chars[i-1] > '0')
                dp[i] = dp[i-1];
            //如果当前的数字可以和前面的数字组合成一个字母
            if( chars[i-2] < '2' || (chars[i-2] == '2' && chars[i-1] <= '6' ) )
                dp[i] += dp[i-2];
        }
        return dp[chars.length];
    }

    public static void main(String args[]){
        String test = "1234";
        System.out.println(countDecodingDp(test));
    }

}

时间复杂度为O(n), 空间复杂度也为O(n)。空间复杂度也可以优化为O(1),因为只是依赖前面的两个值。

参考:http://www.geeksforgeeks.org/count-possible-decodings-given-digit-sequence/


  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  2. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  3. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)