Decode Ways
https://leetcode.com/problems/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.
类似于爬梯子,对于第i位,其解码方式可能为:
1)如果该位不为0,则可以继续按照i-1位的方式解码
2)如果该位和前一位的值在10和26之间,则可继续按照i-2位的方式解码
class Solution {
public:
int numDecodings(string s) {
if(s.empty())return ;
int len=s.size();
vector<int> num(len+,);
num[]=;
for(int i=;i<=len;i++)
{
if(s[i-]!='')
num[i]+=num[i-];
if(i>= && s.substr(i-,)>="" && s.substr(i-,)<="")
num[i]+=num[i-];
}
return num.back();
}
};
Decode Ways的更多相关文章
- [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] ...
- leetcode 91 Decode Ways I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- Decode Ways leetcode java
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
随机推荐
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- jQuery Ajax传值给Servlet,在Servlet里Get接受参数乱码的解决方法
最近在学jquery ui,在做一个小功能的时候需要将前台的值获取到,通过Ajax传递给Servlet,然后再在返回数据结果,但是在Servlet接受参数的时候,通过后台打印,发现接受乱码,代码示例如 ...
- "Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated )
"Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated ...
- SQL Server 2012附加数据库时,错误提示如下:尝试打开或创建物理时,CREATE FILE 遇到操作系统错误 5(拒绝访问。)
错误提示:Create File遇到操作系统错误5(拒绝访问) 解决方案: 在所有程序-SQL Server 2012-"SQL Server 配置管理器",点击"SQL ...
- 基本组件的使用——UITabBarController
和UINavigationController的作用差不多,UITabBarController也可以在多个UIViewController中切换 这个控件的使用相对简单,只需要为该控件的viewCo ...
- 利用powerDesigner16.5连接oracle数据库并自动生成表结构图
- xv6课本翻译之——第0章 操作系统接口
Chapter 0 第0章 Operating system interfaces 操作系统接口 The job of an operating system is to share a comput ...
- List Map Set 的用法和区别
参考 http://blog.csdn.net/vstar283551454/article/details/8682655
- ios小数向上、下取整,计算结果向上、下取整
[摘要:小数背上与整,指小数局部间接进1 x=3.14, ceilf (x)=4 小数背下与整,指间接往失落小数局部 x=3.14,floor(x)=3 盘算效果背上与整 A被除数,B除数 ,(AB- ...
- BZOJ1192 [HNOI2006]鬼谷子的钱袋
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...