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.

class Solution {
public:
bool check(char a, char b) {
int na = a - '0', nb = b - '0';
if(na == 0) return false;
if(na*10 + nb >= 1 && na*10 + nb <= 26) return true;
return false;
}
int numDecodings(string s) {
if(s.length() == 0) return 0; vector<int> dp(s.length(), 0);
dp[0] = (s[0]=='0')? 0: 1;
if(s.length() == 1) return dp[0]; if(dp[0] == 0) return 0;
else {
bool flag = check(s[0], s[1]);
if(s[1] == '0' && flag) dp[1] = 1;
else if(s[1] == '0' && !flag) return 0;
else if(s[1] != '0' && flag) dp[1] = 2;
else if(s[1] != '0' && !flag) dp[1] = 1;
} for(int i=2;i<s.length();++i) {
if(s[i] == '0') {
if(check(s[i-1], s[i])) dp[i] = dp[i-2];
else return 0;
}
else {
if(check(s[i-1], s[i])) dp[i] = dp[i-1] + dp[i-2];
else dp[i] = dp[i-1];
}
} return dp[s.length()-1];
}
};

leetcode 91: Decode Ways

 

leetcode@ [91] Decode Ways (Dynamic Programming)的更多相关文章

  1. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  2. 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 ...

  3. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  4. leetcode 91 Decode Ways ----- java

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  5. [leetcode]91. Decode Ways解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  6. Leetcode#91 Decode Ways

    原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...

  7. [LeetCode] 639. Decode Ways II 解码方法 II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  8. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  9. 91. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

随机推荐

  1. 谈谈js中for in 需要注意的地方

    js中for in 可以遍历对象或数组的显性属性,也就是说我们自己定义的属性是可以遍历的,那些原型上默认已有的属性,例如:Object.prototype.toString.Object.protot ...

  2. python:Attempted relative import in non-package

    problem:Attempted relative import in non-package 所谓相对路径其实就是相对于当前module的路径,但如果直接执行脚本,这个module的name就是“ ...

  3. linux mysql数据库安装(tar.gz)

    概述 mysql数据库在linux下可以充分发挥威力,mysql数据库越来越受到软件公司的青睐,为什么呢? 免费.跨平台.轻.支持多并发 在北京很多软件公司属于创业型的中.小公司,从节约成本的角度考虑 ...

  4. JS插件excanvas的使用方法

     这个还没有想好怎么写,等写好后再发布 试用了excanvas.js,生成静态统计图 IE下使用excanvas.js的注意事项

  5. win7打开或关闭windows功能 提示“出现错误,并非所有的功能被更改”,管理员权限惹的祸

    2013-07-25 18:12:06 最近要用到windows的telnet功能,本来是很简单的事情,因为管理员权限的问题,花了不少时间,才发现是管理员权限惹的祸,更滑稽的是,自己一直以来都不是管理 ...

  6. Fast scroller styles

    <!-- Fast scroller styles --> <!-- Drawable to use as the fast scroll thumb. --> <att ...

  7. 深入解析字符串的比较方法:“==”操作符;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。

    1:要判断2个字符串变量是否相等,最高效的方法是看它们是否指向相同的内存地址.前面使用RefernceEquals方法来比较.如果2个变量指向的是不同的内存地址,那么就需要逐字符的比较2个字符串的变量 ...

  8. DOCTYPE, HTML和XHTML, Strict DTD和Transitional DTD, Quirks Mode和Standard Mode

    在HTML里面声明DOCTYPE一般会有以下几种: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  9. xcopy 复制了0个文件

    xcopy /Y "..\..\..\SolutionItems\zbmyuncore.db" "..\ZITaker" 复制zbmyuncore.db文件的时 ...

  10. awesome awesomeness

    Awesome Awesomeness A curated list of amazingly awesome awesomeness.Also available on: Awesome-Aweso ...