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()];
代码:
1 class Solution { 2 public: 3 int numDecodings(string s) { 4 if (s.size() == 0) { 5 return 0; 6 } 7 int dp[s.size() + 1]; 8 dp[0] = 1; 9 if (s[0] != '0') { 10 dp[1] = 1; 11 } 12 for (int i = 2; i <= s.size(); ++i) { 13 if (s[i - 1] == '0') { 14 if (s[i - 2] == '1' || s[i - 2] == '2') { 15 dp[i] = dp[i - 2]; 16 continue; 17 } 18 else { 19 return 0; 20 } 21 } 22 if (s[i - 2] == '1' || (s[i - 2] == '2' && s[i - 1] <= '6') ) { 23 dp[i] = dp[i - 1] + dp[i - 2]; 24 } 25 else { 26 dp[i] = dp[i - 1]; 27 } 28 } 29 return dp[s.size()]; 30 } 31 };
2. 递推关系:
转载于:https://www.cnblogs.com/wangxiaobao/p/5994727.html
LeetCode91 Decode Ways相关推荐
- [LeetCode] Decode Ways
(Version 0.0) Decode Ways这道题从原理上说是一个比较简单的一维DP题目,用一个一维数组的元素dp[i] (i >= 1)来记录从头开始长度为i的substring有多少种 ...
- Decode Ways leetcode java
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 【重点!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-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 ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 91 Decode Ways
为什么我常常对做题产生恐惧,因为可能为了一个不算难的问题不知不觉绕进去2个小时,这显然是不值得的.这题就是如此. 还要注意,java && 的优先级高于||的优先级,而不是同级. pu ...
- 91 Decode Ways
91 Decode Ways dp解法 O(1) space class Solution:# @param {string} s# @return {integer}def numDecodings ...
最新文章
- SAP Retail 寄售门店关键配置
- Python初学者请注意!别这样直接运行python命令,否则电脑等于“裸奔”
- [云炬创业基础笔记]第十一章创业计划书测试3
- “无人化时代”正在逼近,网友:再不努力就无工可打啦!
- 【Java学习笔记十】输入输出流
- Atitit .h5文件上传 v3
- linux vim配置缩减,让VIM更好的工作——VIM基本配置
- netty权威指南第三版_Hadoop权威指南(第二版及第三版)
- 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码
- 竖排书A5双面打印设置指南
- 微信数据清空了怎么办?误删微信聊天记录怎么恢复
- 12、URL后端编码解码工具
- 2019计算机保研经验记录:南开软院、武大、信工所、南大软院、哈工深
- 中兴新支点操作系统_中兴新支点操作系统下载
- 有什么适合小团队的协作工具?
- PDF办公技巧之PDF怎么删除其中一页
- 用Linux开发板制作智能音箱,【工程师实战】只要几步,普通音箱秒变小度智能音箱...
- 从新东方讲师到AI框架工程师,我的历次职业转折|OneFlow U
- 生产制造管理系统MES
- CAN光端机在西门子FC18/720消防主机海上风电厂之星型联网方式