[leetcode]91. 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 a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
思路:
解法一:recursion, 找到全部可能的解密方法(会TLE)
time : O(2 ^ n) 因为是一个二叉树, 其高度最高为n, 所以时间复杂度是O(2 ^ n)
space: O(n) 跟数的高度有关,就是n层
public int numDecodings(String s ){
if(s == null || s.length() == 0) retrun 0;
return numDecodings(s.toCharArray(), 0);
}
private int numDecodings(char[] array, int level){
if(level == array.length){return 1;}
int ways = 0;
if(array[level] != ‘0’) {
ways += numDecodings(array, level + 1);
}
if(validEncoding(array, level)){
ways += numDecodings(array, level + 2);
}
return ways;
}
private boolean validEncoding(char[]array, int start){
if(start + 1 >= array.length) return false;
if(array[start] = ‘1’) return true;
if(array[start] == ‘2’ && array[start + 1] –‘6’ <=0) return true;
return false;
}
解法二:自顶向下记忆化搜索(recursion + memorization)
time : O(1) * (n + 1 ) = O(n)
space: O(n)
class Solution {
public int numDecodings(String s ){
if(s == null || s.length() == 0) return 0;
int[] m = new int[s.length() + 1];
Arrays.fill(m, -1);
return numDecodings(s.toCharArray(), 0, m);
}
private int numDecodings(char[] array, int level, int[] m){
if(m[level] != -1){
return m[level];
}
if(level == array.length){
m[level] = 1;
return 1;
}
int ways = 0;
if(array[level] != '0') {
ways += numDecodings(array, level + 1, m);
}
if(validEncoding(array, level)){
ways += numDecodings(array, level + 2, m);
}
return ways;
}
private boolean validEncoding(char[]array, int start){
if(start + 1 >= array.length) return false;
if(array[start] == '1') return true;
if( array[start] == '2' && array[start + 1] - '6' <= 0 ){
return true;
}
return false;
}
}
解法三: 自底向上的dp
class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0) return 0;
int len = s.length();
int[] dp = new int[len + 1];
dp[0] = 1;
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for (int i = 2; i <= len; i++) {
int first = Integer.parseInt(s.substring(i - 1, i));
int second = Integer.parseInt(s.substring(i - 2, i));
if (first >= 1 && first <= 9) {
dp[i] += dp[i - 1];
}
if (second >= 10 && second <= 26) {
dp[i] += dp[i - 2];
}
}
return dp[len];
}
}
[leetcode]91. Decode Ways解码方法的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [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 I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- [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' - ...
- [LintCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...
- Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
随机推荐
- Android引入动态库so的方法
Android引入动态库so的方法 标签(空格分隔): Android so 第三方库 为了执行效率,会将一些CPU密集性任务如音视频解码.图像处理等放入到so中,还有也会将程序关键核心部分放入到so ...
- 20165308 2017-2018-2 《Java程序设计》课程总结
20165308 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告链接汇总 我期待的师生关系 学习基础和c语言调查 Linux 安装及学习 第一周学习总结 第二 ...
- WINDOWS NT操作系统的注册表文件
WINDOWS NT操作系统的注册表文件 WINDOWS NT注册表文件分为系统文件和用户文件两类. 系统设置和缺少用户 配置数据存放在系统C:\Windows\System32\config文件夹下 ...
- 对于使用JDBC连接mysql数据时The server time zone value '¤¤°ê¼Ð·Ç®É¶¡'...的异常问题解决。
相信很多小伙伴和我一样遇到了这类问题,在使用JDBC连接mysql数据库的时候发生SQLException如下所示的异常情况! java.sql.SQLException: The server ti ...
- 变式配置简介 VARIANT CONFIGURATION
变式物料类型KMAT; ITEM CATEGORY GROUP: main item 0002 sub item 0004 strategy group:25 requirement type: ke ...
- U3D学习12-黑暗之光实例
1.static勾选后,在scene场景操作后,导致不断烘焙,cpu占用高? 取消自动烘焙 2.UI操作事件 //监听事件增加 mainInputField.onValueChange ...
- Chapter4 复杂度分析(下):浅析最好,最坏,平均,均摊时间复杂度
四个复杂度分析: 1:最好情况时间复杂度(best case time complexity) 2:最坏情况时间复杂度(worst case time complexity) 3:平均情况时间复杂度( ...
- 深入了解scanf()/getchar()和gets()/cin等函数
转:http://www.cnblogs.com/FCWORLD/archive/2010/12/04/1896511.html 转:问题描述一:(分析scanf()和getchar()读取字符) s ...
- css3兼容性检测工具
1. Modernizr 会在Modernizr 会在页面加载后立即检测特性:然后创建一个包含检测结果的 JavaScript 对象,同时在 html 元素加入方便你调整 CSS 的 class ...
- 白话RPC
RPC,这个英文缩写在计算机专业里的意思是:Remote Procedure Call Protocol,远程过程调用协议,字面上的意思就是这个,不过还是有些懵逼. 下面就简单说明一下其内在原理,形象 ...