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.

就是要注意0的出现

class Solution {
public:
int numDecodings(string s) {
int len = s.size();
if(len==)
return ;
vector<int> num(len,);//第i个位置记录从s的第0个位置开始到第i个位置共有几种解码方式
if(len>= && s[]=='')
return ; num[] = ;
if(len==)
return num[]; string s0 = s.substr(,);
int d = sToInt(s0);
int n=;
if(d> && d<= && d!=)
num[] = ;
else if((s[] =='' && (s[]==''|| s[]==''))||s[]!='')
num[] = num[];
else
return ; for(int i=;i<len;i++){
if(s[i-]==''){
if(s[i]!=''){
num[i] = num[i-];
continue;
}
else
return ;
} s0 = s.substr(i-,);
d = sToInt(s0);
if(s[i]==''){
if(d== || d==){
num[i] = num[i-];
continue;
} else
return ;
} if(d> && d<= )
num[i] = num[i-]+num[i-];
else
num[i] = num[i-]; }//end for
return num[len-];
}
private:
int sToInt(string s){
istringstream is(s);
int d;
is>>d;
return d; }
};

[LeetCode] Decode Ways(DP)的更多相关文章

  1. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  2. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  3. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  4. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

  5. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  6. Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)

    Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...

  7. Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)

    Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...

  8. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

  9. Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)

    Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...

随机推荐

  1. Java unicode中文编码转换和反转

    参考网址http://www.oschina.net/code/snippet_142385_4297 http://canofy.iteye.com/blog/718659 在java的很多配置文件 ...

  2. 【BZOJ】2321: [BeiJing2011集训]星器(数学+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2321 完全想不到.. 第一眼以为是爆搜,看到数据范围果断放弃:第二眼以为是网络流(因为只有行列操作, ...

  3. JSON.stringify语法解析(自己留存)

    语法: JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为2 ...

  4. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

  5. Graph database_neo4j 底层存储结构分析(5)

    3.5 Property 的存储 下面是neo4j graph db 中,Property数据存储对应的文件: neostore.propertystore.db neostore.propertys ...

  6. 理解with(nolock)(转载)

    本文导读:要 提升SQL的查询效能,一般来说大家会以建立索引(index)为第一考虑.其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH (NOLOCK)可以改善 ...

  7. Struts2 中result type属性说明

    Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...

  8. Linux 高级网络编程

    设置套接字函数: #include<sys/socket.h> int setsockopt(int sockfd, int level, int optname, const void* ...

  9. IOS第四天(4:下一题和选框的实现)

    #import "HMViewController.h" #import "HMQuestion.h" #define kButtonWidth 35 #def ...

  10. Leetcode | Parentheses 相关

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...