一条包含字母 A-Z 的消息通过以下方式进行了编码:

'A' -> 1 'B' -> 2 ... 'Z' -> 26

给定一个只包含数字的非空字符串,请计算解码方法的总数。

示例 1:

输入: "12" 输出: 2 解释: 它可以解码为 "AB"(1 2)或者 "L"(12)。

示例 2:

输入: "226" 输出: 3 解释: 它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。

class Solution {
public:
int numDecodings(string s)
{
int len = s.size();
if(len == 1)
return s[0] == '0'? 0 : 1;
if(s[0] == '0')
return 0;
vector<int> dp(len, 0);
dp[0] = 1;
int x = (s[0] - '0') * 10 + s[1] - '0';
if(s[1] == '0' && s[0] <= '2' && s[0] >= '1')
dp[1] = 1;
else if(s[1] == '0' && s[0] > '2')
return 0;
else if(x <= 26 && x >= 1)
{
dp[1] = 2;
}
else
dp[1] = 1;
for(int i = 2; i < len; i++)
{
int x = (s[i - 1] - '0') * 10 + s[i] - '0';
if(x == 0)
return 0;
else if(s[i - 1] == '0')
{
dp[i] = dp[i - 1];
}
else if(s[i] == '0' && s[i - 1] <= '2' && s[i - 1] >= '1')
{
dp[i] = dp[i - 2];
}
else if(s[i] == '0' && s[i - 1] > '2')
{
return 0;
}
else if(x <= 26 && x >= 1)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
else if(x > 26)
dp[i] = dp[i - 1];
}
return dp[len - 1];
}
};

Leetcode91.Decode Ways解码方法的更多相关文章

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

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

  2. [LeetCode] Decode Ways 解码方法

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

  3. [LeetCode] Decode Ways 解码方法个数、动态规划

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

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

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

  5. [LintCode] Decode Ways 解码方法

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

  6. 091 Decode Ways 解码方法

    包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...

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

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

  8. 【leetcode-91 动态规划】 解码方法

    一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...

  9. [LeetCode] decode ways 解码方式

    A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...

随机推荐

  1. Java英语面试题(核心知识篇)

    Java英语面试题(核心知识篇) Question: What is transient variable?Answer: Transient variable can't be serialize. ...

  2. sql自定义日期函数,返回范围内日期和星期数表。

    Create function [dbo].[FUN_GenerateTime] ( @begin_date datetime, -- 起始时间 @end_date datetime -- 结束时间 ...

  3. atoi和itoa函数的实现方法

    atoi的实现: #include<iostream> using namespace std; int atio1(char *s) { int sign=1,num=0; if(*s= ...

  4. vue生成条形码/二维码/带logo二维码

    条形码:https://blog.csdn.net/dakache11/article/details/83749410 //安装 cnpm install @xkeshi/vue-barcode / ...

  5. [JLOI2015]战争调度【暴力+树形Dp】

    Online Judge:Bzoj4007,Luogu P3262 Label:暴力,树形Dp 题解 参考了这篇blog https://www.cnblogs.com/GXZlegend/p/830 ...

  6. 两台linux 服务器同步

    准备: 主服务器 192.168.0.1 备份服务器 192.168.0.2 备份服务器 注意需要开放873端口 而且小心selinux 开始: sudo vim /etc/rsyncd.passwd ...

  7. 简单的sequence unpacking

    t = (1, 2, ‘hl’) x, y, z = t 上述方法可用于任何sequence

  8. phalcon常用语法

    打印SQL //在config/service.php中注册服务 $di->set( 'profiler', function () { return new \Phalcon\Db\Profi ...

  9. js的深复制与浅复制

    什么是深复制和浅复制? 深复制和浅复制的概念只存在于对象array和数组obj上. 浅复制是:模糊复制,就是不管对方是字符串类型还是引用类型都通通复制过来.结果两个变量的内容会同时变化. 深复制是:有 ...

  10. php表单字段

    在上一章节我们已经介绍了表的验证规则,我们可以看到"Name", "E-mail", 和 "Gender" 字段是必须的,各字段不能为空. ...