题目

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;  
10         else 
11             dp[1] = 0; 
12         
13         for(int i=2; i<=s.length();i++){  
14             if (isValid(s.substring(i-1,i)))  
15                 dp[i] += dp[i-1];  
16             if (isValid(s.substring(i-2,i)))  
17                 dp[i] += dp[i-2];  
18         }  
19         return dp[s.length()];  
20     }  
21       
22     public boolean isValid(String s){  
23         if (s.charAt(0)=='0') 
24             return false;  
25         int code = Integer.parseInt(s);  
26         return code>=1 && code<=26;  
27     } 

Reference:

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

Decode Ways leetcode java相关推荐

  1. 【重点!DP】LeetCode 639. Decode Ways II

    LeetCode 639. Decode Ways II 参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-d ...

  2. 【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways

    LeetCode 91. Decode Ways Solution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了... class Solution { ...

  3. [LeetCode]91.Decode Ways

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

  4. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  5. [LeetCode] Decode Ways

    (Version 0.0) Decode Ways这道题从原理上说是一个比较简单的一维DP题目,用一个一维数组的元素dp[i] (i >= 1)来记录从头开始长度为i的substring有多少种 ...

  6. 91 Decode Ways

    为什么我常常对做题产生恐惧,因为可能为了一个不算难的问题不知不觉绕进去2个小时,这显然是不值得的.这题就是如此. 还要注意,java && 的优先级高于||的优先级,而不是同级. pu ...

  7. [LeetCode-Algorithms-91] Decode Ways (2017.10.19-WEEK7)

    题目链接:Decode Ways 题目描述:A message containing letters from A-Z is being encoded to numbers using the fo ...

  8. 91 Decode Ways

    91 Decode Ways dp解法 O(1) space class Solution:# @param {string} s# @return {integer}def numDecodings ...

  9. LeetCode 91 Decode Ways(编码方式)(*)

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

最新文章

  1. c 普通的文本变成注释文本的快捷键_IntelliJ Idea 常用快捷键列表 (2019年总结)
  2. 服务器能否只做c盘系统,我的云服务器只有一个c盘
  3. 应该算是在说 delphi 的日志框架吧
  4. SurfaceView类透明背景设置
  5. JavaScript开发中几个常用知识点总结
  6. SUSE12SP3-Mycat(4)rule.xml配置详解
  7. java B2B2C Springcloud仿淘宝电子商城系统
  8. Latex表格中内容过长换行方法
  9. 过滤程序的html代码,值得收藏的html过滤代码
  10. Linux SocketCan client server demo hacking
  11. 服务器租用如何保证数据安全
  12. CentOS6u9 网卡HWADDR和UUID信息重新生成和获取
  13. 运行 Android 的笔记本 Cosmo 已众筹超 130 万美元
  14. 使用python来完成数据的线性拟合
  15. Centos 7.6 挂载硬盘
  16. HashMap的底层原理你真的知道?
  17. 骆昊python100天 github_GitHub - zj775230/Python-100-Days: Python - 100天从新手到大师
  18. Magento 数据表结构 EAV模型详解
  19. android下面res目录
  20. error: conflicting declaration ‘typedef struct LZ4_stream_t LZ4_stream_t’

热门文章

  1. vue+element+echarts柱状图+列表
  2. path.join 与 path.resolve 的区别
  3. np.identity()
  4. 差分数组 and 树上差分
  5. 在一台win10上启动多个mysql
  6. Keil4 几例异常解决办法
  7. ActiveMQ 实现消息接收发送
  8. java中怎么进行字符串替换?
  9. Sharepoint学习笔记—Site Definition系列-- 5、List Definition与List Template之比较
  10. 为了保护眼睛,请调节颜色