Decode Ways leetcode java
题目:
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]的时候要判断两种可能性,再累加。
代码如下:
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相关推荐
- 【重点!DP】LeetCode 639. Decode Ways II
LeetCode 639. Decode Ways II 参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-d ...
- 【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways
LeetCode 91. Decode Ways Solution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了... class Solution { ...
- [LeetCode]91.Decode Ways
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [LeetCode] Decode Ways
(Version 0.0) Decode Ways这道题从原理上说是一个比较简单的一维DP题目,用一个一维数组的元素dp[i] (i >= 1)来记录从头开始长度为i的substring有多少种 ...
- 91 Decode Ways
为什么我常常对做题产生恐惧,因为可能为了一个不算难的问题不知不觉绕进去2个小时,这显然是不值得的.这题就是如此. 还要注意,java && 的优先级高于||的优先级,而不是同级. pu ...
- [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 ...
- 91 Decode Ways
91 Decode Ways dp解法 O(1) space class Solution:# @param {string} s# @return {integer}def numDecodings ...
- LeetCode 91 Decode Ways(编码方式)(*)
原文 A message containing letters from A-Z is being encoded to numbers the following mapping: 'A' -> ...
最新文章
- c 普通的文本变成注释文本的快捷键_IntelliJ Idea 常用快捷键列表 (2019年总结)
- 服务器能否只做c盘系统,我的云服务器只有一个c盘
- 应该算是在说 delphi 的日志框架吧
- SurfaceView类透明背景设置
- JavaScript开发中几个常用知识点总结
- SUSE12SP3-Mycat(4)rule.xml配置详解
- java B2B2C Springcloud仿淘宝电子商城系统
- Latex表格中内容过长换行方法
- 过滤程序的html代码,值得收藏的html过滤代码
- Linux SocketCan client server demo hacking
- 服务器租用如何保证数据安全
- CentOS6u9 网卡HWADDR和UUID信息重新生成和获取
- 运行 Android 的笔记本 Cosmo 已众筹超 130 万美元
- 使用python来完成数据的线性拟合
- Centos 7.6 挂载硬盘
- HashMap的底层原理你真的知道?
- 骆昊python100天 github_GitHub - zj775230/Python-100-Days: Python - 100天从新手到大师
- Magento 数据表结构 EAV模型详解
- android下面res目录
- error: conflicting declaration ‘typedef struct LZ4_stream_t LZ4_stream_t’