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、Android构建本地单元测试
如果你的单元测试在Android中没有依赖或者只有简单的以来,你可以在你的本地开发环境中运行你的测试.这种测试比较高效因为它能让你避免将整个app安装到物理设备或虚拟机中执行单元测试.最后,执行单元测 ...
- web中间件切换(was切tomcat)
一.数据源迁移: ①数据源配置在web容器还是在项目本身? 根据开发与生产分离原则选择配置到web容器,以免开发泄露数据库密码. ②数据库密码加密 原先was的数据源直接在console控制,密码是密 ...
- 头部——MimeHeaders
http协议的请求头部更像一个键值对,例如Content-Length : 123,前面为键后面为值,表示文本长度为123.对于若干个头部在请求对象中被封装成MimeHeaders对象,MimeHea ...
- Elcipse安装gradle插件
参考: http://www.gradle.org/docs/current/userguide/installation.html (1)下载Gradle 官网下载www.g ...
- hive支持in用法是从0.3.2版本后
写hive 用in 如分时段,分类型,分平台统计点击量 select substr(createtime,12,2) hour,logtype,os_id,count(*) from wizad_ ...
- (NO.00004)iOS实现打砖块游戏(二):实现游戏主界面动画
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个内容不错的游戏也要一个好的包装.玩家进入游戏时第一眼看到的是 ...
- MySQL数据库内置函数
mysql数据库中提供了很丰富的函数.mysql函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操作. 简单介绍几类函数的 ...
- 网站开发进阶(三十四)编码中的setCharacterEncoding 理解
编码中的setCharacterEncoding 理解 1.pageEncoding="UTF-8"的作用是设置JSP编译成Servlet时使用的编码. 2.contentType ...
- Eclipse插件 - FindBugs 检查代码隐藏的 Bug
简介 FindBugs 是一个在 Java 程序中查找 bug 的程序,它可以查找可能出错的代码,注意 FindBugs 是检查 Java 字节码,也就是*.class文件.其实准确的 ...
- STL:set/multiset用法详解
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...