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: ' ...
随机推荐
- 解读ASP.NET 5 & MVC6系列(17):MVC中的其他新特性
(GlobalImport全局导入功能) 默认新建立的MVC程序中,在Views目录下,新增加了一个_GlobalImport.cshtml文件和_ViewStart.cshtml平级,该文件的功能类 ...
- php测试
Php基础知识测试题 姓名: 班级: 成绩: 本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A ) A:Windo ...
- C# Winform代码片段-大二下学期的垃圾代码
1.图片缩放 using System; using System.Windows.Forms; using System.Drawing; class haha : Form { static vo ...
- 敏捷组织中PMO应遵循的准则
敏捷改变了人们的工作方式,不仅仅是开发部门,而且还包括其它的部门,例如HR.财务以及PMO等.在大多数组织中,PMO是一个控制体.它指导项目团队的规范.模板以及流程.目前,大多数的IT组织都敏捷化了. ...
- c# Entity DbArithmeticExpression arguments must have a numeric common type
在Entity Lambda表达式中计算DateTime类型的时候 不能直接用DateTime类型进行相减计算差值 需要使用DbFunctions提供的一些方法. 例如: DbFunctions.Di ...
- codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Nginx配置性能优化
大多数的Nginx安装指南告诉你如下基础知识--通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...
- Mui.ajax请求服务器正确返回json数据格式
ajax: mui.ajax('http://server-name/login.php',{ data:{ username:'username', password:'password' }, d ...
- [转]CSS 类名的单词连字符:下划线还是横杠?
问题 CSS 类或 ID 命名时单词间连接通常有这几种写法: 驼峰式: solutionTitle.solutionDetail 用横杠连接: solution-title.solution-deta ...
- 博文Contents<1--到200—>
====================-------------- 前言:博客中的随笔文章.并非都是笔者的原创文章.有些是听别人说的.有些是书上摘录的.有些是百度的.有些是别人博客的文章.有些是自己 ...