leetcode 639 Decode Ways II
首先回顾一下decode ways I 的做法:链接
分情况讨论
if s[i]=='*'
考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1
再考虑s[i-1]和s[i]decode,如果s[i-1]为*,那么**的组合共有15种(11,12,13.....,21,22..26),注意不是9*9=81种,因为我们是s[i-1]和s[i]一起decode,如果是38这样是大于26的
如果s[i-1]不为*,枚举s[i],组合起来小于26即可
if s[i]!='*'
考虑s[i]单独decode, 和decode ways I 一样,考虑s[i]为0的情况
再考虑s[i-1]和s[i] decode 如果s[i-1]为*, 那么 *和s[i]组合要小于26,直接枚举判断就行了
如果s[i-1]不为*,枚举s[i-1],组合起来小于26即可
不用条件运算符会更快,但用了代码会简洁很多
class Solution {
public:
int numDecodings(string s) {
int n=s.length();
if(n==) return ;
vector<int> dp(n+,);
long long dp1=,dp2=,now;
if(s[]!='') dp1=s[]=='*'?:;
for(int i=;i<n;i++){
if(s[i]=='*'){
now=dp1*;
if(s[i-]=='*') now+=i-<?:dp2*;
else{
for(int j=;j<;j++){
if(s[i-]!=''&&((s[i-]-'')*+j<=)) now+=i-<?:dp2;
}
}
}
else{
now=s[i]==''?:dp1;
if(s[i-]=='*'){
for(int j=;j<;j++){
if(j*+s[i]-''<=) now+=i-<?:dp2;
}
}
else{
if(s[i-]!=''&&((s[i-]-'')*+s[i]-''<=)) now+=i-<?:dp2;
}
}
dp2=dp1%();
dp1=now%();
}
return (int)dp1;
}
};
leetcode 639 Decode Ways II的更多相关文章
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 639. Decode Ways II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 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 (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- [Swift]LeetCode639. 解码方法 2 | Decode Ways II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 【leetcode】Decode Ways(medium)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- 2017-12-19python全栈9期第四天第二节之列表的增删查改之正向排序和倒向排序和反转
#!/user/bin/python# -*- coding:utf-8 -*-li = [3,5,6546,6,8,324,2,1,34,5,6,7]# li.sort() #正向# print(l ...
- GO语言系列(一)- 初识go语言
一.golang语言的特性 1.垃圾回收 a.内存自动回收,再也不需要开发人员管理内存 b.开发人员专注业务实现,降低了心智负担 c.只需要new分配内存,不需要释放 2.天然并发 a.从语言层面支持 ...
- Three ways to detect outliers
Z-score import numpy as np def outliers_z_score(ys): threshold = 3 mean_y = np.mean(ys) stdev_y = np ...
- logging模块简单使用
日志配置 #!/usr/bin/python2.7 import os import logging def get_logger(path='./', filename='access.log', ...
- [C++]数据结构-排序:插入排序之直接插入排序
得赶紧休息了,木有时间写原理了.直接上代码. /* <插入排序-直接插入排序> */ #include<iostream> using namespace std; void ...
- 算法时间计算:logA(N)与O(n)
算法运行时间估算常见O(log(n))log:求对数例:a^b=na为底数,b为n的对数记作:logA(n)=b ->求N的对数 计算器验算:计算器的log默认以10为底 输入 10,log ...
- SQL Server安全
第一篇 SQL Server安全概述 第二篇 SQL Server安全验证 第三篇 SQL Server安全主体和安全对象 第四篇 SQL Server安全权限 第五篇 SQL Server安全架构和 ...
- mac office2016
- Docker入门-安装(一)
Docker 在CentOS 7.0下安装Docker, CentOS 7.0默认使用的是firewall作为防火墙 查看防火墙状态 firewall-cmd --state 停止firewall ...
- LabVIEW--使用云端编译器编译多个vi
使用ni 云服务器编译vi 详细请看链接: https://users.niwsc.com/compilecloud/#/ http://www.ni.com/white-paper/52328/en ...