LeetCode91 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. (Medium)
分析:
开始想到的就是暴力搜索,但是超时了,所以考虑用动态规划来做。
符合单序列动态规划的特点,所以有
1. 状态:dp[i]表示前i个字符组成的子串可能的解码方式。
2. 初始化:dp[0] = 1, dp[1] = 1(s[0] != '0',因为没有0这个对应数字)
3. 递推关系:
一般情况下:
if s[i - 1]与s[i -2]组成的在“1” ~“26”范围内, dp[i] = dp[i - 1] + dp[i - 2];
else, dp[i] = dp[i - 1];
但要注意的是,如果s[i - 1] == '0'需要特殊处理,当s[i - 2] == '1' || '2'时, dp[i] = dp[i - 2]
否则,则说明这个0没有对应位,这个字符串无法解码,直接返回0;
4.结果: dp[s.size()];
代码:
class Solution {
public:
int numDecodings(string s) {
if (s.size() == ) {
return ;
}
int dp[s.size() + ];
dp[] = ;
if (s[] != '') {
dp[] = ;
}
for (int i = ; i <= s.size(); ++i) {
if (s[i - ] == '') {
if (s[i - ] == '' || s[i - ] == '') {
dp[i] = dp[i - ];
continue;
}
else {
return ;
}
}
if (s[i - ] == '' || (s[i - ] == '' && s[i - ] <= '') ) {
dp[i] = dp[i - ] + dp[i - ];
}
else {
dp[i] = dp[i - ];
}
}
return dp[s.size()];
}
};
2. 递推关系:
LeetCode91 Decode Ways的更多相关文章
- Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LintCode] 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 ...
- 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- leetcode面试准备:Decode Ways
1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- leetcode 639 Decode Ways II
首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...
随机推荐
- python中os的常用方法
1.os模块:os模块在python中包含普遍的操作系统功能,下面列出了一些在os模块中比较有用的部分. os.sep可以取代操作系统特定的路径分隔符.windows下为 “\\” os.name字符 ...
- Shell脚本编程中截取字符串方法
例如: 假设变量var=http://www.baidu.com/111.png 1.#号截取(删左留右) echo ${var#*//} # 号是运算符,*// 表示从左边开始删除第一个 // 号及 ...
- TZ_16_Vue_入门案例
1.新建一个html文件导入vue.js <script src="node_modules/vue/dist/vue.js"></script> 2.创建 ...
- SpringBoot随机数
# 随机字符串 com.didispace.blog.value=${random.value} # 随机int com.didispace.blog.number=${random.int} # 随 ...
- Wireless Network POJ - 2236 (并查集)
#include<iostream> #include<vector> #include<string> #include<cmath> #includ ...
- Git同账号多平台配置
最近工作中使用到了Git,虽然以前学习过,但是已经忘的差不多了,遂将本次配置过程整理成笔记以备忘 生成公钥 ssh-keygen -t rsa -C "gana10007@163.com&q ...
- nyoj zb的生日【背包型DFS/选or不选】
zb的生日 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...
- oracle-视图-索引-序列
Oracle提高查询性能 一 视图 视图是一个虚拟表,就是对select查询的结果取个名字.其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值 ...
- java-异常进阶-包的使用
一 finally 1.1 异常执行的顺序 package test; public class Test { public static void main(String[] args) { Dem ...
- UE4物理模块(三)---碰撞查询(上)
在前一文中介绍了如何在UE4中创建简单碰撞或者直接使用其mesh表示的复杂碰撞: Jerry:UE4物理模块(二)---建立物体碰撞zhuanlan.zhihu.com 那么在拿到碰撞之后,就可以进 ...