512-解码方法

有一个消息包含A-Z通过以下规则编码

'A' -> 1

'B' -> 2

...

'Z' -> 26

现在给你一个加密过后的消息,问有几种解码的方式

样例

给你的消息为12,有两种方式解码 AB(12) 或者 L(12). 所以返回 2

标签

动态规划 字符串处理

思路

使用动态规划,用一维数组 dp[i] 表示加密消息中前 i 位有多少种解法。因为有效的解在 1 - 26 之间,所以要注意有效解(可能会出现无解的消息,如 100 )特别是 0 的出现。

code

class Solution {
public:
/*
* @param s: a string, encoded message
* @return: an integer, the number of ways decoding
*/
int numDecodings(string s) {
// write your code here
int size = s.size();
if (size <= 0) {
return 0;
}
vector<int> dp(size + 1, 0);
dp[0] = 1;
for (int i = 1; i <= size; i++) {
if (s[i - 1] == '0') {
dp[i] = 0;
}
else {
dp[i] += dp[i - 1];
}
if (i > 1) {
int num = atoi(s.substr(i - 2, 2).c_str());
if (num >= 10 && num <= 26) {
dp[i] += dp[i - 2];
}
}
}
return dp[size];
}
};

lintcode-512-解码方法的更多相关文章

  1. [LeetCode] Decode Ways 解码方法

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

  2. C#中Base64之编码,解码方法

    原文:C#中Base64之编码,解码方法 1.base64  to  string string strPath =  "aHR0cDovLzIwMy44MS4yOS40Njo1NTU3L1 ...

  3. [LeetCode] Decode Ways II 解码方法之二

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

  4. [Swift]LeetCode91. 解码方法 | Decode Ways

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

  5. [Swift]LeetCode639. 解码方法 2 | Decode Ways II

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

  6. LeetCode(91):解码方法

    Medium! 题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计 ...

  7. leetcode 91. 解码方法 JAVA

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

  8. 使用多字节字符集的跨平台(PC、Android、IOS、WP)编码/解码方法

    随着移动端的发展,跨平台已成为通讯架构设计的重要考虑因素,PC.Android.IOS.WP等跨多平台间的数据通讯,必然要解决字符编码/解码的问题. 多字节字符集MBCS不是跨平台的首选字符集,面向跨 ...

  9. LeetCode OJ:Decode Ways(解码方法)

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

  10. 【算法题12 解码方法decode way】

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

随机推荐

  1. 我的 Delphi 学习之路 —— Delphi 的安装

    标题:我的 Delphi 学习之路 -- Delphi 的安装 作者:断桥烟雨旧人伤 1. Delphi 版本的选择 Delphi 版本众多,我该选择哪一个,这确实是个问题,自从 Borland 公司 ...

  2. 项目-高性能TcpServer - 目录

    1.项目-高性能TcpServer - 1.网络通信协议 https://blog.csdn.net/arno1988/article/details/82463225 2.项目-高性能TcpServ ...

  3. 搭建最小linux系统

    Busybox简介 • 制作文件系统我们需要使用到Busybox 工具 – 版本为busybox-1.21.1.tar.bz2 – 开源网址是http://www.busybox.net/ – Bus ...

  4. nRF52832 BLE_DFU空中升级OTA(二)编译下载(SDK14.2.0)

    上一篇配置好了开发环境,现在就可以试着跑一下例程了,这里需要两个例程,一个是bootloader的,一个是应用程序的,其路径分别为: bootloader:SDK_14.2.0工程\examples\ ...

  5. python自动化学习

    1.环境搭建 1.1 下载或拷贝整个PyAuthoTest到指定的目录,如D:\PyAuthoTest 1.2 安装Python2.7以及需要使用到的安装包列表如下 requests-2.10.0.t ...

  6. 关于LP64,ILP64,LLP64,ILP32,LP32字长(数据)模型

    太长不看: 1.32位Windows和类Unix使用ILP32字长模型,64位Windows使用ILP64模型,64位类Unix使用LP64字长模型. 2.根据1,long在32位和64位Window ...

  7. ACM1020:Encoding

    Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...

  8. 搜索引擎的选择与在chrome上的设置

    1  优缺点分析 百度:广告多,但是电脑端可以用Adblock Plus屏蔽:搜索内容有很多百度自家内容,如百家号.百度知道.百度文库.百度贴吧等,在搜索教程的时候很实用,但是不适合偏专业性搜索,很多 ...

  9. Flex4中的拖动技术

    下面列一个最简单的例子,在Flex中,拖动原来如此简单 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?xm ...

  10. 莫队算法&BZOJ2038

    题目传送门 今天看了分块,顺便把基本的莫队学习了一下. 莫队算法是一种离线算法,复杂度可以达到O((M+N)*Sqrt(N)); 对于询问区间的左端点分块,块内的右端点从小到大排列. 对区间进行偏移操 ...