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. CodeForces Round 199 Div2

    完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions     # When Who Problem Lang Verdict Time Memory 443 ...

  2. lua ipairs

    tbl = {"alpha", "beta", ["one"] = "uno", ["two"] = ...

  3. HTML6 展望

    HTML5 概述 HTML5 是 HTML 语言最受欢迎的版本之一,它支持音频和视频.离线存储.移动端.和标签属性等等.还提供了<article>, <section>, &l ...

  4. Codeforces Round #213 (Div. 2) A. Good Number

    #include <iostream> #include <vector> using namespace std; int main(){ ; cin >> n ...

  5. POJ 3373 Changing Digits(DP)

    题目链接 记录路径的DP,看的别人的思路.自己写的也不好,时间居然2000+,中间的取余可以打个表,优化一下. 写的各种错,导致wa很多次,写了一下午,自己构造数据,终于发现了最后一个bug. dp[ ...

  6. COJ1012 WZJ的数据结构(十二)

    今天突然想写个树套树爽一爽(1810ms) 写的是树状数组套线段树(动态开节点) #include<cstdio> #include<cctype> #include<c ...

  7. PS如何查找自己想要的字体

    曾经很多人问我,这是什么字体,答曰:“不知道”,自己平时在作图时也经常会遇到这样的问题,于是我就把文字打出来,然后比照PS里边已经导入的字体挨个试验,这样不仅浪费了大量的时间和精力,而且比对完后,各种 ...

  8. PHP统计字符串里单词查询关键字

    <?function full_count_words($str) {     //返回完整数组,包含字符串里每个单词 $words = str_word_count($str,1);     ...

  9. [转]C#基础回顾:Asp.net 缓存

    本文转自http://www.cnblogs.com/stg609/archive/2009/03/22/1418992.html 缓存的作用      你买电脑的时候,是否会在意CPU的二级缓存?是 ...

  10. 从网页上抓取Windows补丁信息然后整型输出(PowerShell)

    $report = [pscustomobject]@{'Date' = $null; 'MSRC' = $null; 'KB' = $null; 'Severity' = $null; 'Versi ...