题目

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. P1828 香甜的黄油 Sweet Butter

    对于这道洛谷ac而我整了一下午的codevs的题,我也是很绝望啊. 原因是队列数组开小了我勒个去???我说STL怎么能过 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧 ...

  2. Node.js开源应用OSN发布初始V1.0版本-见面版本

    Nodejs开源应用OSN初始版本V1.0发布,请参考本操作说明文档,有任何问题请留言 Nodejs开源应用OSN发布V1.0版本: OSChina收录地址: OSC收录地址:http://www.o ...

  3. [Java] jdk与jre的区别

    很多程序员已经干了一段时间java了依然不明白jdk与jre的区别.JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环境. ...

  4. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  5. .Net中常用的重要的第三方组件

    RSS.NET.dll RSS.NET是一款操作RSS feeds的开源.NET类库.它为解析和编写RSS feeds提供了一个可重用的对象模型.它完全兼容RSS 0.90, 0.91, 0.92, ...

  6. LPC43xx OTP

  7. Android论坛

    APKBUS:http://www.apkbus.com/forum.php 看雪ANDROID:http://bbs.pediy.com http://www.52pojie.cn http://w ...

  8. MySQL 5.6主从Slave_IO_Running:Connecting/error connecting to master *- retry

    刚配置的MySQL主从,在从机上看到 点击(此处)折叠或打开 mysql> SHOW slave STATUS \\G *************************** 1. row ** ...

  9. Lucene 3.0 输出相似度

    http://www.cnblogs.com/ibook360/archive/2011/10/19/2217638.html Lucene3.0之结果排序(原理篇) 传统上,人们将信息检索系统返回结 ...

  10. GO -- 正则表达式

    str := "880218end" match, _ := regexp.MatchString("\\d{16}", str) //六位连续的数字 fmt. ...