091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:
'A' -> 1
'B' -> 2
...
'Z' -> 26
给定一个包含数字的编码消息,请确定解码方法的总数。
例如,
给定消息为 "12", 它可以解码为 "AB"(1 2)或 "L"(12)。
"12" 的解码方法为 2 种。
详见:https://leetcode.com/problems/decode-ways/description/
Java实现:
class Solution {
public int numDecodings(String s) {
if (s.length()==0||s.isEmpty()||s.equals("0")){
return 0;
}
int[] dp = new int[s.length()+1];
dp[0] = 1;
if (isValid(s.substring(0,1))){
dp[1]=1;
}else{
dp[1]=0;
}
for(int i=2; i<=s.length();i++){
if (isValid(s.substring(i-1,i))){
dp[i]+=dp[i-1];
}
if (isValid(s.substring(i-2,i))){
dp[i]+=dp[i-2];
}
}
return dp[s.length()];
}
public boolean isValid(String s){
if (s.charAt(0)=='0') {
return false;
}
int code = Integer.parseInt(s);
return code>=1 && code<=26;
}
}
详见:https://www.cnblogs.com/springfor/p/3896162.html
091 Decode Ways 解码方法的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [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' - ...
- [LintCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 【LeetCode】091. 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 fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...
随机推荐
- HDU3183 A Magic Lamp —— 贪心(单调队列优化)/ RMQ / 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题解: 方法一:贪心. 在草稿纸上试多几次可以知道,删除数字中从左到右最后一位递增(可以等于)的 ...
- hadoop,帮我解了部分惑的文章
http://blog.csdn.net/qianshangding0708/article/details/47423613
- Android-Universal-Image-Loader使用介绍
简介 Android上最让人头疼的莫过于从网络获取图片.显示.回收,任何一个环节有问题都可能直接OOM,这个项目或许能帮到你.Universal Image Loader for Android的目的 ...
- html5--4-1 video/视频播放
html5--4-1 video/视频播放 学习要点 掌握video元素的基本用法 直到现在,在网页中的大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件. HTM ...
- hdu-5768 Lucky7(容斥定理+中国剩余定理)
题目链接: Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pr ...
- 使用boost库生成 随机数 随机字符串
#include <iostream> #include <boost/random/random_device.hpp> #include "boost/rando ...
- c/c++生成预编译文件
Preprocesses C and C++ source files and writes the preprocessed output to a file. /P Remarks The f ...
- 【EOJ Monthly 2018.2 (Good bye 2017)】
23333333333333333 由于情人节要回家,所以就先只放代码了. 此题是与我胖虎过不去. [E. 出老千的 xjj] #include<cstdio> #include<c ...
- KVM虚拟机内无agent情况下的监控方法
KVM虚拟机内无agent情况下的监控(ceilometer实现) 今天看到大家在群里讨论KVM虚拟机的监控问题,而且是要求VM内无agent情况下的监控.这方面确实没有深入研究,但尚有些openst ...
- CF 345A Mike and Frog
题目 自己歪歪的做法WA了好多发. 原题中每一秒都相当于 $x1 = f1(x1)$ $x2 = f2(x2)$ 然后这是一个定义域和值域都在[0,m-1]的函数,显而易见其会形成一个环. 而且环长不 ...