Java for 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.
解题思路:
dp问题,JAVA实现如下:
static public int numDecodings(String s) {
if (s.length() == 0 || s.charAt(0) == '0')
return 0;
int[] dp = new int[s.length() + 1];
dp[0] = dp[1] = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == '0'&& (s.charAt(i - 1) != '1' && s.charAt(i - 1) != '2'))
return 0;
else if (s.charAt(i) == '0')
dp[i + 1] = dp[i - 1];
else if (s.charAt(i - 1) == '0'||(s.charAt(i-1)-'0')*10+(s.charAt(i)-'0')>26)
dp[i + 1] = dp[i];
else
dp[i + 1] = dp[i - 1] + dp[i];
}
return dp[s.length()];
}
Java for LeetCode 091 Decode Ways的更多相关文章
- [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 (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】091. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- [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]题解(python):091 Decode Ways
题目来源 https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encod ...
- leetcode 91 Decode Ways ----- java
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- xampp 安装 mysql-python
在已经安装brew前提下:brew install mysql-connector-c pip install MySQL-python
- 研读:Shielding applications from an untrusted cloud with Haven
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHJ1c3Ribw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- Web支持可暂停的超大文件上传
代码镇顶:https://github.com/dna2github/petalJS/blob/master/upload 前些天遇到用户须要上传10GB大小以上的文件的需求,查查网上的库.都不好用. ...
- openfire常见几类插件开发研究与总结
openfire 的插件可以访问所有openfire的API,这给我们的插件实现提供了巨大的灵活性. 以下介绍几类比较常用的插件集成方式: 基于源码XMPP协议的插件 比如:IQHandler,常用来 ...
- zoj3329--One Person Game(概率dp第六弹:形成环的dp,带入系数,高斯消元)
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- 伪静态对struts action的重写
参见 http://ocaicai.iteye.com/blog/1312189 最重要的而是在web.xml中配置 <filter-mapping> <filter-name> ...
- UNP学习笔记(第十六章 非阻塞I/O)
套接字的默认状态时阻塞的 可能阻塞的套接字调用可分为以下4类: 1.输入操作,包括read.readv.recv.recvfrom和recvmsg. 2.输入操作,包括write.writev.sen ...
- UNP学习笔记(第六章 I/O复用)
I/O模型 首先我们将查看UNIX下可用的5种I/O模型的基本区别: 1.阻塞式I/O 2.非阻塞式I/O 3.I/O复用(select和poll) 4.信号驱动式I/O(SIGIO) 5.异步I/O ...
- 数据存储之Archiver、Unarchiver、偏好设置
数组的归档 对象的归档 NSData多个对象的归档 NSArray多个对象的归档 偏好设置的存储 1.NSString.NSDictionary.NSArray.NSData.NSNumber等类型的 ...
- WikiCFP--A Wiki for Calls For Papers
WikiCFP--A Wiki for Calls For Papers ---->www.wikicfp.com/cfp/