[LintCode] Decode Ways 解码方法
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.
Example
Given encoded message 12, it could be decoded as AB (1 2) or L (12).
The number of ways decoding 12 is 2.
LeetCode上的原题,请参见我之前的博客Decode Ways。
解法一:
class Solution {
public:
/**
* @param s a string, encoded message
* @return an integer, the number of ways decoding
*/
int numDecodings(string& s) {
if (s.empty()) return ;
int n = s.size();
vector<int> dp(n + , );
for (int i = ; i < n + ; ++i) {
if (s[i - ] == '') dp[i] = ;
else dp[i] = dp[i - ];
if (i >= && (s[i - ] == '' || (s[i - ] == '' && s[i - ] <= ''))) {
dp[i] += dp[i -];
}
}
return dp.back();
}
};
解法二:
class Solution {
public:
/**
* @param s a string, encoded message
* @return an integer, the number of ways decoding
*/
int numDecodings(string& s) {
if (s.empty()) return ;
vector<int> dp(s.size() + , );
dp[] = ;
for (int i = ; i < dp.size(); ++i) {
if (s[i - ] >= '' && s[i - ] <= '') dp[i] += dp[i - ];
if (i >= && s.substr(i - , ) <= "" && s.substr(i - , ) >= "") {
dp[i] += dp[i - ];
}
}
return dp.back();
}
};
解法三:
class Solution {
public:
/**
* @param s a string, encoded message
* @return an integer, the number of ways decoding
*/
int numDecodings(string& s) {
if (s.empty() || s.front() == '') return ;
int c1 = , c2 = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] == '') c1 = ;
if (s[i - ] == '' || (s[i - ] == '' && s[i] <= '')) {
c1 = c1 + c2;
c2 = c1 - c2;
} else {
c2 = c1;
}
}
return c1;
}
};
[LintCode] Decode Ways 解码方法的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways 解码方法个数、动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...
- Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] decode ways 解码方式
A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
随机推荐
- visio调整画布大小和旋转画布(转)
1.调整画布大小: 鼠标移至画布边界(注意不能是顶点附近),按下ctrl,就会出现双向箭头,拖动鼠标即能调整画布大小. 2.旋转画布: 鼠标移至画布顶点或附近,按下ctrl, 出现单箭头优弧,移动鼠标 ...
- HDU 2242 考研路茫茫——空调教室 无向图缩环+树形DP
考研路茫茫——空调教室 Problem Description 众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们.Lele也是其中一个.而某教室旁边又摆着两个未装上的空调,更 ...
- document.body.scrollTop
标准浏览器:document.documentElement.scrollTop; 谷歌浏览器:document.body.scrollTop; var scrollTop = document.do ...
- LoadRunner IP欺骗(转)
直接转了篇运用LR来实现IP欺骗的文章. http://www.cnblogs.com/fnng/archive/2013/03/02/2940284.html
- express-9 Handlebars模板引擎(2)
视图和布局 视图通常表现为网站上的各个页面(它也可以表现为页面中AJAX局部加载的内容,或一封电子邮件,或页面上的任何东西).默认情况下,Express会在views子目录中查找视图.布局是一种特殊的 ...
- unity 解析xml
using UnityEngine; using System.Collections; using System.IO; using System.Xml; public class xml : M ...
- AngularJS进阶学习
参考:http://***/class/54f3ba65e564e50cfccbad4b 1. AJAX:Asynchronous JavaScript and XML(异步的 JavaScript ...
- FastDFS原理
转自:http://blog.chinaunix.net/uid-20196318-id-4058561.html 开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器 ...
- POJO(PO)与javaBean的比较、以及DTO的说明
一.POJO(Plain Ordinary Java Object)简单的Java对象,其中有一些属性及其getter setter方法的类,没有业务逻辑(重点理解一下"没有业务逻辑&quo ...
- Js apply 方法 详解
Js apply方法详解 我在一开始看到JavaScript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这 ...