639. Decode Ways II
A message containing letters from A-Z
is being encoded to numbers using the following mapping way:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Beyond that, now the encoded string can also contain the character '*', which can be treated as one of the numbers from 1 to 9.
Given the encoded message containing digits and the character '*', return the total number of ways to decode it.
Also, since the answer may be very large, you should return the output mod 109 + 7.
Example 1:
Input: "*"
Output: 9
Explanation: The encoded message can be decoded to the string: "A", "B", "C", "D", "E", "F", "G", "H", "I".
Example 2:
Input: "1*"
Output: 9 + 9 = 18
Note:
- The length of the input string will fit in range [1, 105].
- The input string will only contain the character '*' and digits '0' - '9'.
Approach #1: DP. [C++]
class Solution {
int mod = 1000000007; public int numDecodings(String s) {
if (s.isEmpty()) return 0;
long[] dp = new long[2];
dp[0] = 1;
dp[1] = ways(s.charAt(0));
// int ans = 0;
for (int i = 1; i < s.length(); ++i) {
long ans = ways(s.charAt(i)) * dp[1] + ways(s.charAt(i-1), s.charAt(i)) * dp[0];
ans %= mod;
dp[0] = dp[1];
dp[1] = ans;
}
return (int)dp[1];
} public int ways(char c) {
if (c == '*') return 9;
if (c == '0') return 0;
return 1;
} public int ways(char c1, char c2) {
if (c1 == '*' && c2 == '*') return 15;
if (c1 == '*')
if (c2 >= '0' && c2 <= '6') return 2;
else return 1;
else if (c2 == '*')
if (c1 == '1') return 9;
else if (c1 == '2') return 6;
else return 0;
else {
int num = (c1 - '0') * 10 + (c2 - '0');
if (num >= 10 && num <= 26) return 1;
else return 0;
} }
}
Reference:
http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/
639. Decode Ways II的更多相关文章
- leetcode 639 Decode Ways II
首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- [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: ' ...
- Decode Ways II
Description A message containing letters from A-Z is being encoded to numbers using the following ma ...
- 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 ...
- 91. Decode Ways反编译字符串
[抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- [LeetCode] 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' - ...
随机推荐
- async与await
在方法上可以加 async,方法体内需要有 await,没有await的话,会出现warn警告.async单独出现是没有用的. await只能出现在Task前面.await Task的后面的代码会被封 ...
- Golang之Socket
go创建socket很简单 package main import ( "fmt" "net" ) func main() { //服务器监听地址 fmt.Pr ...
- ubuntu and centos各种上网代理设置
1.Ubuntu 一. apt-get 代理设置 由于公司通过代理上网,firefox的代理设置很容易就搞定了,但是通过apt-get安装软件还是不行,于是,查阅了很多资料,最多的方法就是网上流传的三 ...
- Spring.NET学习笔记8——集合类型的注入(基础篇)
1.基础类 public class Happy { public override string ToString() { return &q ...
- 向一个文件流写入一个数据块---fwrite
函数原型:int fwrite(const void *buffer,size_t size,size_t count,FILE *stream); 参数说明:buffer:用于写入到文件的数据地址. ...
- vue移动端h5页面根据屏幕适配的四种方案
最近做了两个关于h5页面对接公众号的项目,不得不提打开微信浏览器内置地图导航的功能确实有点恶心.下次想起来了的话,进行总结分享一下如何处理.在vue移动端h5页面当中,其中适配是经常会遇到的问题,这块 ...
- java读取网页图片路径并下载到本地
java读取网页图片路径并下载到本地 最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且 将图片的路径保存到数据库,示例代码如下: packa ...
- 2018.09.17 bzoj1260: [CQOI2007]涂色paint(区间dp)
传送门 区间dp简单题啊. 很显然用f[l][r]f[l][r]f[l][r]表示把区间[l,r][l,r][l,r]按要求染好的代价. 这样可以O(n)O(n)O(n)枚举断点转移了啊. 显然如果断 ...
- 2018.08.31 bzoj3566: [SHOI2014]概率充电器(概率dp+容斥原理)
传送门 概率dp好题啊. 用f[i]" role="presentation" style="position: relative;">f[i] ...
- Linux 系统运维常用命令
1 文件管理2 软件管理3 系统管理4 服务管理5 网络管理6 磁盘管理7 用户管理8 脚本相关9 服务配置==================================------------ ...