LeetCode之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.
跟Climbing Stairs非常类似。只是多加一些推断逻辑。
class Solution {
public:
int numDecodings(string s) {
if(s.empty() ||s[0]=='0') return 0;
int cur_2=1,cur_1=1,cur=0;
for(int i=2;i<=s.size();i++){
if(s[i-1]!='0') cur+=cur_1;
if(s[i-2]=='1'||s[i-2]=='2'&& s[i-1]<='6')
cur+=cur_2;
cur_2=cur_1;
cur_1=cur;
cur=0;
}
return cur_1;
}
};
LeetCode之Decode Ways的更多相关文章
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- 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 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [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 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- leetcode 639 Decode Ways II
首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...
- 【leetcode】Decode Ways(medium)
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 OJ] 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' - ...
随机推荐
- hadoop1.2.1+zk-3.4.5+hbase-0.94.1集群安装过程详解
hadoop1.2.1+zk-3.4.5+hbase-0.94.1集群安装过程详解 一,环境: 1,主机规划: 集群中包括3个节点:hadoop01为Master,其余为Salve,节点之间局域网连接 ...
- JVM 入门三板斧
一个JVM实例只存在一个堆内存,堆内存的大小是可以调节的.类加载器读取了类文件后,需要把类.方法.常变量放到堆内存中,保存所有引用类型的真实信息,以方便执行器执行,堆内存分为三部分: Young Ge ...
- xpath的常见操作
1. 获取某一个节点下所有的文本数据: data = response.xpath('//div[@id="zoomcon"]') content = ''.join(data.x ...
- php分享二十一:mysql语句
一.Join语法概述 JOIN 按照功能大致分为如下三类: INNER JOIN(内连接,或等值连接):取得两个表中存在连接匹配关系的记录. LEFT JOIN(左连接) RIGHT JOIN(右连接 ...
- 在 Unity 中基于 Oculus DK1 的开发
开发环境: Windows 10 专业版 64位(GeForce GTX 970M,驱动版本:378.72) 大朋助手 1.3.2.10,大朋E2(http://www.deepoon.com/dap ...
- eclipse 创建Maven 架构的dynamic web project 问题解决汇总
Eclipse创建Maven结构的web项目的时候选择Artifact Id为maven-artchetype-webapp,点击finish之后,一般会遇到如下问题 1. The superclas ...
- 部署到Google App Engine时中途退出后引起的问题
如果部署GAE时正在upload files时退出,下次部署时会报错 Another transaction by user is already in progress for this app a ...
- 服务端 https和SSL
String keyStoreFilePassword = System .getProperty("keystore.file.password"); /usr/java/jdk ...
- 50x页面放到本地单独目录下,进行显示
error_page 500 502 503 504 /50x.html; location = /50x.html { root /data0/www/html; }
- redis 指定IP访问
配置IPTABLES -A INPUT -s 10.100.0.5 -p tcp --dport 6379 -j ACCEPT -A INPUT -s 10.100.0.219 -p tcp --dp ...