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 ...
随机推荐
- hdu 1047 Integer Inquiry(大数)
题意:整数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<stdlib.h> #include ...
- Apktool 和 Jeb 给出的不同的smali语法
今天发现用Apktool和Jeb反编译出来的smali在语法上有一定区别,比如一个Java函数: private void packageNameCheck() { com.example.testf ...
- HDU3440 House Man (差分约束)
In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. To ...
- BZOJ_5416_[Noi2018]冒泡排序_DP+组合数+树状数组
BZOJ_5416_[Noi2018]冒泡排序_DP+组合数+树状数组 Description www.lydsy.com/JudgeOnline/upload/noi2018day1.pdf 好题. ...
- AtCoder Grand Contest 012 D:Colorful Balls
题目传送门:https://agc012.contest.atcoder.jp/tasks/agc012_d 题目翻译 给你一排一共\(N\)个球,每个球有一个颜色\(c_i\)和一个重量\(w_i\ ...
- DTP模型之二:(XA协议之二)jotm分布式事务实现
分布式事务是指操作多个数据库之间的事务,spring的org.springframework.transaction.jta.JtaTransactionManager,提供了分布式事务支持.如果使用 ...
- keil5中文乱码的解决
keil5 复制出来的中文显示乱码,该如何解决? 点击Edit - Configuration ,进入编辑器设置: 点击ok ,就可以了
- plsql 免oracle客户端安装
PLSQL Developer 11.0.0.1762 中文绿色注册版(免Oracle11g客户端) 免安装Oracle客户端,绿色无公害. 说明: 1.点击 "启动PLSQL.exe&qu ...
- storyBoard学习教程一(页面跳转)
今天为了给伙伴作一篇storyBoard快速编程的教程,所以才写下了这篇博客. 有过storyBoard 编程经验的伙伴还是不要阅读本篇博客了,我自己认为,太基础太简单了,为了方便别人学习使用,我还是 ...
- studio 集成 Genymotion后打开模拟器出错、打开虚拟机VirtualBox出错
好吧,首先给出错误类型如下: “Unable to load R3 module D:\Program Files\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastErr ...