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解码方法的更多相关文章

  1. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  2. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  3. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  4. 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 ...

  5. [LeetCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  6. [LeetCode] Decode Ways 解码方法个数、动态规划

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  7. [LintCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. 091 Decode Ways 解码方法

    包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...

  9. Leetcode91.Decode Ways解码方法

    一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...

随机推荐

  1. 使用vue自定义简单的消息提示框

    <style scoped> /** 弹窗动画*/ a { text-decoration: none } .drop-enter-active { /* 动画进入过程:0.5s */ t ...

  2. CMake for MFC example

    cmake_minimum_required(VERSION 2.8) add_definitions(-D_AFXDLL) set(CMAKE_MFC_FLAG 2) set(SRCS MFCApp ...

  3. python的set处理二维数组转一维数组

    for splitValue in set(dataset[:, featureIndex].tolist()): 首先set是一个无序,无重复的数据结构,所以很多时候使用它来进行去重:但是set接收 ...

  4. SQL查:询结果区分大小写

    在MS SQL2005中的方法: 1)select * from user where name collate Chinese_PRC_CS_AS like 'A$%B%' escape '$'; ...

  5. [综] meanshift算法

    Meanshift,聚类算法 http://www.cnblogs.com/liqizhou/archive/2012/05/12/2497220.html 记得刚读研究生的时候,学习的第一个算法就是 ...

  6. 原生js创建模态框(摘自:东窗凝残月 链接:https://www.cnblogs.com/dcncy/p/9076937.html)

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Te ...

  7. bananapi+OLED做的一个打地鼠游戏,c语言编程

    说明一下:BPI是对拍死的BPI的计数,对应最终的成绩RANK是难度 数值越低难度越高 每当打死10个BPI以后就会减一即难度高一级 默认初始化RANK等于15 DIE是存在的BPI数量,一旦数量大于 ...

  8. VS2010自定义添加创建者、创建时间等个人信息新建文件模版

    不知不觉VS2010已经成为.NET开发人员的必备工具,相比经典版VS2005,到过渡版vs2008,2010在性能稳定性和易用性上都得到很大的提高. 结合VS工具,其下的插件也层出不穷.今天重点给大 ...

  9. 使用Java监控工具出现 Can't attach to the process

    问题重现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ➜ jinfo -flags 3032 Attaching ...

  10. 静态初始化块和main方法哪个先被执行?

    直接看代码 public class BlockAndMain { public static void main(String[] args) { System.out.println(" ...