[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层

 1 public int numDecodings(String s ){
 2     if(s == null || s.length() == 0) retrun 0;
 3     return numDecodings(s.toCharArray(), 0);
 4 }
 5 
 6 private int numDecodings(char[] array, int level){
 7     if(level == array.length){return 1;}
 8     int ways = 0;
 9     if(array[level] != ‘0’) {
10         ways += numDecodings(array, level + 1);
11   }
12   if(validEncoding(array, level)){
13       ways += numDecodings(array, level + 2);
14   }
15     return ways;
16 }
17 
18 private boolean validEncoding(char[]array, int start){
19     if(start + 1 >= array.length) return false;
20     if(array[start] = ‘1’) return true;
21     if(array[start] == ‘2’ && array[start + 1] –‘6’ <=0) return true;
22     return false;
23 }

 

解法二:自顶向下记忆化搜索(recursion + memorization)

time :  O(1) * (n + 1 )  = O(n)

space: O(n)

 1 class Solution {
 2       public int numDecodings(String s ){
 3         if(s == null || s.length() == 0) return 0;
 4         int[] m = new int[s.length() + 1];
 5         Arrays.fill(m, -1);
 6         return numDecodings(s.toCharArray(), 0, m);
 7     }
 8 
 9     private int numDecodings(char[] array, int level, int[] m){
10         if(m[level] != -1){
11             return m[level];
12         }
13         if(level == array.length){
14             m[level] = 1;
15             return 1;
16         }
17         int ways = 0;
18         if(array[level] != '0') {
19             ways += numDecodings(array, level + 1, m);
20         }
21         if(validEncoding(array, level)){
22             ways += numDecodings(array, level + 2, m);
23         }
24         return ways;
25     }
26 
27     private boolean validEncoding(char[]array, int start){
28         if(start + 1 >= array.length) return false;
29         if(array[start] == '1') return true;
30         if( array[start] == '2' && array[start + 1] - '6' <= 0 ){
31             return true;
32         }
33         return false;
34     }
35 }

 

解法三: 自底向上的dp

 1 class Solution {
 2      public int numDecodings(String s) {
 3         if (s == null || s.length() == 0) return 0;
 4         int len = s.length();
 5         int[] dp = new int[len + 1];
 6         dp[0] = 1;
 7         dp[1] = s.charAt(0) == '0' ? 0 : 1;
 8         for (int i = 2; i <= len; i++) {
 9             int first = Integer.parseInt(s.substring(i - 1, i));
10             int second = Integer.parseInt(s.substring(i - 2, i));
11             if (first >= 1 && first <= 9) {
12                 dp[i] += dp[i - 1];
13             }
14             if (second >= 10 && second <= 26) {
15                 dp[i] += dp[i - 2];
16             }
17         }
18         return dp[len];
19     }
20 }

 

 

 

转载于:https://www.cnblogs.com/liuliu5151/p/9070217.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值