【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 ...
随机推荐
- win10 1709正式版iso镜像下载|windows10 1709秋季创意者更新官方下载地址
win10 1709正式版iso镜像下载|windows10 1709秋季创意者更新官方下载地址 发布时间:2017-10-18 14:27发布者:系统城-xtcjh浏览数:74458 win10 1 ...
- 常用global.css
html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, img { margin: ...
- XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' ' is therefore not allowed access.
ajax跨域 禁止访问! 利用Access-Control-Allow-Origin响应头解决跨域请求
- JavaScript -- 没事看看
@.delete 原文:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/delete
- JBossWeb/Tomcat 初始化连接器和处理 Http 请求过程
概述 JBossWeb 是JBoss 中的 Web 容器.他是对 Tomcat 的封装,本文以 Http 连接器为例.简单说明 JBossWeb/Tomcat 初始化连接器和处理 Http 请求过程 ...
- ClassNotFoundException Log
Studio 运行时异常: Error:Execution failed for task ':app:compileDebugJavaWithJavac'.> Compilation fail ...
- css jquery 实现轮播效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 性能测试--yslow
YSlow YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则YSlow会 ...
- nodejs 版本dockerfile 文件制作,和常用命令
Dockerfile 如下 官方的node6.3的版本有点难下载,建议去网易蜂巢 https://c.163.com/hub pull hub.c.163.com/library/node:6.9 ...
- 转 Spring 组件 <context:component-scan base-pakage="">用法
1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入. <!-- 注解注入 --> <co ...