一条包含字母 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. windows环境下,svn未备份情况下重新恢复

    公司有个同事在未打招呼的情况下把公司服务器进行重新装系统,崩溃啊.SVN之前未备份,还好SVN的库(Repositories)还在,如下图: 恢复办法如下: 由于之前安装的就是VisualSVN-Se ...

  2. 70 二叉树的层次遍历 II

    原题网址:http://www.lintcode.com/zh-cn/problem/binary-tree-level-order-traversal-ii/ 给出一棵二叉树,返回其节点值从底向上的 ...

  3. 47 Majority Element II

    原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...

  4. java中自己对页面跳转问题的一些经验

    在eclipse中,如果你要在jsp页面跳转到servlet页面中,可以用action=“/根文件名/servlet文件名” 的方式跳转. 例如我创建了一个web application名字是test ...

  5. .Net Core微服务系列--服务发现

    什么是服务发现 首先我们先思考一个问题,当我们在浏览器中输入一个域名比如baidu.com,然后发生了什么才能让我们访问到百度的网页?简单来说,浏览器会首先从主机的hosts文件中查看是否有baidu ...

  6. Apache Pig入门学习文档(一)

    1,Pig的安装    (一)软件要求    (二)下载Pig      (三)编译Pig 2,运行Pig    (一)Pig的所有执行模式    (二)pig的交互式模式    (三)使用pig脚本 ...

  7. pandas一些基本操作(DataFram和Series)_4

    import numpy as np;import pandas as pd;kill_num=pd.Series([10,12,8,5,0,2,6])#击杀数量#青铜1200-2000#白银2001 ...

  8. Leetcode950. Reveal Cards In Increasing Order按递增顺序显示卡牌

    牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从牌组顶部抽一 ...

  9. 枚举进程,线程,堆 CreateToolhelp32Snapshot

    Takes a snapshot of the processes and the heaps, modules, and threads used by the processes.对当前系统进行一 ...

  10. [转]【全面解禁!真正的Expression Blend实战开发技巧】第六章 认识ListBox

    反反复复考虑后,准备把这一章的切入点瞄准ListBox.并用了一个看起来有点别扭的标题“认识ListBox",许多人看到这里就不爱看了,即使是大学里用winform的学生也会说ListBox ...