leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/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.
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.
class Solution {
public:
bool check(char a, char b) {
int na = a - '0', nb = b - '0';
if(na == 0) return false;
if(na*10 + nb >= 1 && na*10 + nb <= 26) return true;
return false;
}
int numDecodings(string s) {
if(s.length() == 0) return 0; vector<int> dp(s.length(), 0);
dp[0] = (s[0]=='0')? 0: 1;
if(s.length() == 1) return dp[0]; if(dp[0] == 0) return 0;
else {
bool flag = check(s[0], s[1]);
if(s[1] == '0' && flag) dp[1] = 1;
else if(s[1] == '0' && !flag) return 0;
else if(s[1] != '0' && flag) dp[1] = 2;
else if(s[1] != '0' && !flag) dp[1] = 1;
} for(int i=2;i<s.length();++i) {
if(s[i] == '0') {
if(check(s[i-1], s[i])) dp[i] = dp[i-2];
else return 0;
}
else {
if(check(s[i-1], s[i])) dp[i] = dp[i-1] + dp[i-2];
else dp[i] = dp[i-1];
}
} return dp[s.length()-1];
}
};
leetcode 91: Decode Ways
leetcode@ [91] Decode Ways (Dynamic Programming)的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- leetcode 91 Decode Ways I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- leetcode 91 Decode Ways ----- java
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' - ...
- Leetcode#91 Decode Ways
原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
随机推荐
- c缺陷与陷阱笔记-第一章 词法陷阱
1.运算符的贪心性,匹配最长的运算符,例如 n-->0,从-开始,-是运算符,--是运算符,-->就不是,所以是 n -- > 0,--是 a---b,-是,--是,,---不是,所 ...
- 李洪强iOS开发之OC[018]对象和方法之间的关系
// // main.m // 18 - 对象和方法之间的关系 // // Created by vic fan on 16/7/14. // Copyright © 2016年 李洪强. A ...
- ios开发--网页中调用JS与JS注入
先将网页弄到iOS项目中: 网页内容如下, 仅供测试: <html> <head> <meta xmlns="http://www.w3.org/1999/xh ...
- Android:自定义适配器
无论是ArrayAdapter还是SimpleAdapter都继承了BaseAdapter,自定义适配器同样继承BaseAdapter 实例:Gallery实现图片浏览器 <?xml versi ...
- CentOS升级git
1.首先查看下当前的版本 [root@localhost ~]# git --versiongit version 1.8.2.1 2.尝试进行升级 [root@localhost ~]# yum u ...
- 使用ANT打包Android应用
大家好,今天来分享一下如何使用ANT打包Android应用. 通常我们习惯用eclipse来开发Android程序,它会自动帮我们打包当前的应用程序.如果在Navigator视图下,我们可以看到以下几 ...
- sdut 1570 c旅行
用搜索(bfs,dfs)做了半天,都超时,原来是dp; 参考博客:http://www.cnblogs.com/liuzezhuang/archive/2012/07/29/2613820.html ...
- .woff 文件404,配置到web.config
<staticContent> <remove fileExtension=".woff" /> <mimeMap fil ...
- Linux 根文件系统制作
1.创建根文件目录 mkdir rootfs(名字是随便取的) 2.创建子目录 cd rootfs mkdir bin dev etc lib proc sbin sys usr mnt tmp va ...
- C#中哈希表与List的比较
简单概念 在c#中,List是顺序线性表(非链表),用一组地址连续的存储单元依次存储数据元素的线性结构. 哈希表也叫散列表,是一种通过把关键码值映射到表中一个位置来访问记录的数据结构.c#中的哈希表有 ...