decode ways(动态规划)
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.
不用考虑开头是0的情况,题目不会给出这样的数据
这题是动态规划的题,找出递推公式比较难。
跟跳阶梯那个题挺像的。跳阶梯那个题的递推公式很好写出,这个题有点复杂,细节比较多。
思路就是用一个数组来存当前这个位置到最后的解码数。当然也可以使用几个变量。
这里从字符串后面往前推。
第i个元素处,往后的可能性,当第i个元素单独编码,就是1种可能,剩下的元素有dp[i+1]种可能;当第i和i+1个元素符合要求,剩下元素有dp[i+2]中可能。
大的公式就是:dp[i]=dp[i+1]+dp[i+2],这个前提是i元素自己符合要求(1,。9),而且i和i+1组成的数字也符合要求。当i和i+1组成的数字不符合要求时,dp[i]=dp[i+1]。这里没有考虑0的出现。当i出现0,dp[i]默认为0。因为它只有和前面的元素组成10,20才行,这样在前面i元素处,它的方法数就等于dp[i+2]
因为要有初始数据,所以dp数组的长度应该比字符串长度大一。初始值为1.,
class Solution {
public int numDecodings(String s) {
if(s==null||s.length()==0) return 0;
int len=s.length();
int[] dp=new int[len+1];
dp[len]=1;
dp[len-1]=s.charAt(len-1)!='0'?1:0;
for(int i=len-2;i>=0;i--){
if(s.charAt(i)=='0') continue;
else {
dp[i]=((Integer.parseInt(s.substring(i,i+2))<=26))?dp[i+1]+dp[i+2]:dp[i+1];
}
}
return dp[0];
}
}
decode ways(动态规划)的更多相关文章
- Decode Ways——动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 91. Decode Ways(动态规划 26个字母解码个数)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- leetcode面试准备:Decode Ways
1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- Decode Ways leetcode java
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- 2.QT中使用资源文件,程序打包
1 程序中使用资源文件 A 一个QT空项目 B 右击项目,添加新文件 添加后的效果是 C 右击main.prc,选择"添加现有项",找到要使用的资源文件.最终的效果是: ...
- J2EE进阶(三)struts2 <s:action>标签的用法
J2EE进阶(三)struts2 <s:action>标签的用法 前言 使用action标签,可以允许在jsp页面中直接调用Action,(类似AJAX页面调用)在调用Action时候,可 ...
- LCD 显示异常定位分析方法
第一种情况: 进入kernel或android 后,如果LCM图像示异常,可以通过如下步骤来判断问题出现在哪个层面. step1:通过DMMS截图,来判断上面刷到LCM的数据是否有问题. 若DMMS获 ...
- HTML5 placeholder(空白提示) 属性
原文地址:HTML5′s placeholder Attribute 演示地址: placeholder演示 原文日期: 2010年08月09日 翻译日期: 2013年8月6日 浏览器引入了许多的HT ...
- Android官方命令深入分析之etc1tool
etc1tool是一个命令行工具,可以将PNG图像压缩为etc1标准,并且可以进行解压缩. 用法: etc1tool infile [--help | --encode | --encodeNoHea ...
- 07_Android操作sqllite数据库(包括2中方式操作数据的方式),单元测试,BaseAdapter的使用,自定义view的综合使用案例
1 目标从sqllite中读取数据并显示如下: MainActivity对应的界面 MainActivity2对应的界面 2 配置Android的清单文件 <?xml ...
- 【一天一道LeetCode】#33. Search in Rotated Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...
- Libgdx 1.5.2发布
[1.5.2] - 修复问题 #2433 - 修复LWJGL在Mac OS X的本地载入问题 [1.5.1] - Gradle 升级到 2.2 - Android Gradle 工具升级到 1.0.0 ...
- Leetcode_36_Valid Sudoku
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42528601 Determine if a Sudoku ...
- pyhton exit
exit("0") is normally out, and means "successful termination" exit("1" ...