题目

A message containing letters from A-Z is being encoded to numbers using the following mapping:

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12",
it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding "12" is 2.

题解

动态规划来做。

设置动态数组dp[n+1]。dp[i]表示从1~i的decode ways的个数。

当给的code只有一位数时,判断是不是valid(A~Z),是的话就dp[1] = 1 不是的话就是dp[1] = 0

因为像给的例子12可以有两种可能的解析方法,所以计算dp[i]的时候要判断两种可能性,再累加。

代码如下:

 1     public int numDecodings(String s) {  
 2         if (s.length()==0||s==null||s=="0") 
 3             return 0; 
 4 
 5         int[] dp = new int[s.length()+1];  
 6         dp[0] = 1;  
 7         
 8         if (isValid(s.substring(0,1)))
 9             dp[1] = 1;  
         else 
             dp[1] = 0; 
         
         for(int i=2; i<=s.length();i++){  
             if (isValid(s.substring(i-1,i)))  
                 dp[i] += dp[i-1];  
             if (isValid(s.substring(i-2,i)))  
                 dp[i] += dp[i-2];  
         }  
         return dp[s.length()];  
     }  
       
     public boolean isValid(String s){  
         if (s.charAt(0)=='0') 
             return false;  
         int code = Integer.parseInt(s);  
         return code>=1 && code<=26;  
     } 

Reference:

http://blog.csdn.net/u011095253/article/details/9248109

Decode Ways leetcode java的更多相关文章

  1. Decode Ways -- LeetCode

    原题链接: http://oj.leetcode.com/problems/decode-ways/  这道题要求解一个数字串依照字符串编码方式可解析方式的数量.看到这样的求数量的,我们非常easy想 ...

  2. [LeetCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  3. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  4. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  5. [LeetCode] 639. Decode Ways II 解码方法 II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  6. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  7. leetcode面试准备:Decode Ways

    1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  8. [LeetCode] Decode Ways II 解码方法之二

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  9. leetcode 91 Decode Ways I

    令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...

随机推荐

  1. 运行程序,解读this指向---case6

    function Parent() { this.a = 1; this.b = [1, 2, this.a]; this.c = { ckey: 5 }; this.show = function ...

  2. POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  3. 任何一个大于1的自然数n,总可以拆分成若干个小于n的自然数之和。

    任何一个大于1的自然数n,总可以拆分成若干个小于n的自然数之和. 当n=7共14种拆分方法: 7=1+1+1+1+1+1+1 7=1+1+1+1+1+2 7=1+1+1+1+3 7=1+1+1+2+2 ...

  4. BZOJ.5068.友好的生物(思路)

    题目链接 \(Description\) 求\[\max\{\sum_{i=1}^{k-1}(C_i*|a_{x,i}-a_{y,i}|)-C_k*|a_{x,k}-a_{y,k}|\}\] \(So ...

  5. BZOJ.2521.[SHOI2010]最小生成树(最小割ISAP/Dinic)

    题目链接 一条边不变其它边减少可以看做一条边增加其它边不变. 假设要加的边lab为(A->B,v),那么肯定是要使除这条边外,A->B的每条路径上的最小权值都\(>v\),这样在连通 ...

  6. hdu 5203 && BC Round #37 1002

    代码参考自:xyz111 题意: 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:勇太有一根长度为n的木棍,这个木棍是由n个长度为1的小木棍拼接而成,当然由于时间 ...

  7. 使用CefSharp在.Net程序中嵌入Chrome浏览器(六)——调试

    chrome强大的调试功能令许多开发者爱不释手,在使用cef的时候,我们也可以继承这强大的开发者工具. 集成调试: 我们可以使用如下函数直接使用集成在chrome里的开发者工具 _chrome.Sho ...

  8. STM32F4 Alternate function mapping

    #define GPIO_AF0_MCO // MCO (MCO1 and MCO2) Alternate Function mapping #define GPIO_AF0_RTC_50Hz // ...

  9. go标准库DOC与 raft

    http://studygolang.com/static/pkgdoc/index.html https://github.com/avelino/awesome-go#database

  10. STM32F103 TIM3定时器初始化程序

    //TIM3 分频 #define TIM3_DIV1 (1-1) #define TIM3_DIV18 (18-1) #define TIM3_DIV72 (72-1) //************ ...