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. BZOJ2739 : 最远点

    把环倍长,设$w(i,j)$表示对于$i$,决策$j$的价值,如果$j$在$[i,i+n]$,那么$w(i,j)=dis(i,j)$,否则$w(i,j)=-dis(i,j)$. 则$w$满足四边形不等 ...

  2. [转]C++设计模式:Builder模式

    Builder模式要解决的问题是,当我们要创建很复杂的对象时,有时候需要将复杂对象的创建过程和这个对象的表示分离开来.由于在每一步的构造过程中可以映入不同参数,所以步骤相同但是最后的对象却不一样.也就 ...

  3. WebConfig配置文件详解(转载自逆心的博客)

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  4. php如何判断远程文件是否存在

    <?php /*   函数:remote_file_exists   功能:判断远程文件是否存在   参数: $url_file -远程文件URL   返回:存在返回true,不存在或者其他原因 ...

  5. Union大小

    在一个union类型结构中,所有的成员公用同样的存储空间,其占用的大小为其成员中需要空间最大者: union本身只保留一块地址空间,因为只有一个成员真正存储于该地址, 但这块地址也要满足内存对齐原则. ...

  6. [APAC]自动发送邮件

    $lastMon = ((get-date).AddDays(-7) -split(" "))[0] $lastFri = ((get-date).AddDays(-3) -spl ...

  7. 通过SEP禁用USB

    1      Introduction 1.1      Scope This document provides comprehensive information of the reinforce ...

  8. 《GK101任意波发生器》升级固件发布(版本:1.0.2build627)

    一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build672 编译日期:2014-12-29 ====================================== 二. ...

  9. java获取MySQL自动的int类型的Id

    @Resource(name = "dashboardTemplate") protected JdbcTemplate systemJDBCTemplate; //这个是Dao里 ...

  10. [ZZ] Cache

    http://blog.sina.com.cn/s/blog_6472c4cc0102duzr.html 处理器微架构访问Cache的方法与访问主存储器有类似之处.主存储器使用地址编码方式,微架构可以 ...