题目

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. 懒人的福利?教你用set维护斜率优化凸包

    斜率优化题目大家肯定都做得不少了,有一些题目查询插入点的x坐标和查询斜率都不单调,这样就需要维护动态凸包并二分斜率.(例如bzoj1492) 常规的做法是cdq分治或手写平衡树维护凸包,然而如果我不愿 ...

  2. 简表-Java-Echart报表介绍

    Java后台报表尝试了很多,最终发现了一款,而且是开源的,简表地址:http://www.jatools.com/jor/.问题的引入:该报表支持嵌套,钻去,应对excel类似的报表,足够了.但是,报 ...

  3. # 2017-2018-20172309 暑期编程作业:APP

    2017-2018-20172309 暑期编程作业:基于有道词典API的翻译软件的实现. 写在前面:这个博客可以说是拖了很久了.因为做这个APP已经很久了,很多东西都已经忘记了,所以一直都懒得写.但是 ...

  4. URAL 1962 In Chinese Restaurant 数学

    In Chinese Restaurant 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/B Description When ...

  5. iOS 七牛多张图片上传

    -(void)uploadImages:(NSArray *)images atIndex:(NSInteger)index token:(NSString *)token uploadManager ...

  6. AES advanced encryption standard 3

    This optimized <../aesbench/> AES implementation conforms to FIPS-. aes.h #ifndef _AES_H #defi ...

  7. [Go] 单元测试/性能测试 (go test)

    特征 Golang 单元测试对文件名和方法名,参数都有很严格的要求.例如: 1.文件名必须以 _test.go 结尾 2.方法名必须是 Test 开头 3.方法参数必须是 t *testing.T 或 ...

  8. WinForm基于插件开发实现多项配置存储

    一.课程介绍和实例在线演示 明人不说暗话,跟着阿笨一起玩WinForm.本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C ...

  9. 集群服务器下使用SpringBoot @Scheduled注解定时任务

    原文:https://blog.csdn.net/huyang1990/article/details/78551578 SpringBoot提供了 Schedule模块完美支持定时任务的执行 在实际 ...

  10. mormot支持https

    mormot支持https 将ssl证书导入电脑系统,以Windows 10为例: 运行 mmc 证书导入成功后,双击证书,查看证书指纹: 第二项工作:将证书与https绑定:以管理员身份启动cmd, ...