【LeetCode】091. 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.
题解:
这个题与之前的爬楼梯有些类似,只是有了0和26的限制判断。
Solution 1 ()
class Solution {
public:
int numDecodings(string s) {
if(s.empty() || s.size()> && s.front() == '') return ;
vector<int> dp(s.size()+, );
dp[] = ;
for(int i=; i<dp.size(); 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();
}
};
Solution 2 ()
class Solution {
public:
int numDecodings(string s) {
if(s.empty() || s.front() == '') return ;
// r2: decode ways of s[i-2] , r1: decode ways of s[i-1]
int r1 = , r2 = ;
for(int i=; i<s.size(); i++) {
// zero voids ways of the last because zero cannot be used separately
if(s[i] == '') r1 = ;
// possible two-digit letter, so new r1 is sum of both while new r2 is the old r1
if(s[i-] == '' || s[i-] == '' && s[i] <= '') {
r1 = r2 + r1;
r2 = r1 - r2;
}
// one-digit letter, no new way added
else r2 = r1;
}
return r1;
}
};
【LeetCode】091. Decode Ways的更多相关文章
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 【一天一道LeetCode】#91. Decode Ways
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...
- 【LeetCode】241. Different Ways to Add Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归构建所有表达式 方法二:分而治之 日期 ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- 【LeetCode】394. Decode String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- VIM中保存编辑的只读文件
如何在VIM中保存编辑的只读文件 你是否会和我一样经常碰到这样的情景:在VIM中编辑了一个系统配置文件,当需要保存时才发现当前的用户对该文件没有写入的权限.如果已 经做了很多修改,放弃保存的确很懊恼, ...
- 具体解释TCP协议的服务特点以及连接建立与终止的过程(俗称三次握手四次挥手)
转载请附本文的链接地址:http://blog.csdn.net/sahadev_/article/details/50780825 ,谢谢. tcp/ip技术经常会在我们面试的时候出现,非常多公司也 ...
- php eval()计算
php中的eval()函数可以处理php代码,因此可以用此来解决:以字符串格式存储的计算公式 比如: $str='2*(3+12)'; $s=eval("return $str;" ...
- 自动化测试,基于selenium/appnium 学习
1. 搭建环境,配置java,安装tomcat 7.0,运行eclipse
- Unix环境高级编程—进程关系
终端登录 网络登录 进程组 getpgrp(void) setpgid(pid_t pid, pid_) 会话: 是一个或多个进程组的集合,通常由shell的管道将几个进程编成一组. setsid(v ...
- 【BZOJ4408】[Fjoi 2016]神秘数 主席树神题
[BZOJ4408][Fjoi 2016]神秘数 Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1 ...
- 常用BAPI list
2017-03-25 MD 主数据 1.创建物料主数据 CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' 2.创建供应商 CALL METHOD VMD_EI_API=&g ...
- GstAppSink简介
Description Appsink is a sink plugin that supports many different methods for making the application ...
- long_query_time 设置不生效问题
由于原来的慢查询日志太大了,有1G多,并且其中包含上一次查询优化前的慢sql,所以想收集最近两天的慢查询语句,故 mysql> show global variables like 'slow% ...
- empty blank
非nil对象才能调用 empty nil: 对象是否存在empty: ”“ []blank: nil emptypresent: ! blank