LeetCode Decode Ways

原题链接在这里:https://leetcode.com/problems/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 an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding "12" is 2.

题解:

DP题. 要求的是decode的方法. 需要保留的历史信息是到当前字符decode的方法. 用array保存.

递推和等台阶的题目很像. 以"321" 为例,到了第三位时,  s.substring(i-1, i) = "1"是 valid的, s.substring(i-2, i) = "21" 也是valid的.

dp[i] = dp[i-1] + dp[i-2]. 

答案是dp[dp.length-1].

初始化用到前两个, 所以要有两个base case. dp[0] = 1. dp[1]看第一个字符是否是'0', 若不是, dp[0]=1.

Note: 0 非常烦人. 若是string 是"09","10",  对应的数字9, 10都在1到26之间,但“0”根本就不能decode, 所以任何包含 "0"的字符串都是非法的, return false.

Time Complexity: O(n). Space: O(n). n = s.length().

AC Java:

 1 public class Solution {
 2     public int numDecodings(String s) {
 3         if(s == null || s.length() == 0){
 4             return 0;
 5         }
 6         int [] dp = new int[s.length()+1];
 7         dp[0] = 1;
 8         dp[1] = isValid(s.substring(0,1)) ? 1 : 0;
 9         
10         for(int i = 2; i<dp.length; i++){
11             if(isValid(s.substring(i-1, i))){
12                 dp[i] += dp[i-1];
13             }
14             if(isValid(s.substring(i-2, i))){
15                 dp[i] += dp[i-2];
16             }
17         }
18         return dp[dp.length - 1];
19     }
20     
21     private boolean isValid(String s){
22         if(s.charAt(0) == '0'){
23             return false;
24         }
25         return Integer.valueOf(s) >= 1 && Integer.valueOf(s) <= 26;
26     }
27 }

只用到前面两个历史值. 可以降维处理.

Time Complexity: O(s.length()). Space: O(1).

AC Java:

 1 public class Solution {
 2     public int numDecodings(String s) {
 3         if(s == null || s.length() == 0){
 4             return 0;
 5         }
 6         
 7         int first = 1;
 8         int second = isValid(s.substring(0,1)) ? 1 : 0;
 9         
10         for(int i = 2; i<=s.length(); i++){
11             int third = 0;
12             if(isValid(s.substring(i-1, i))){
13                 third += second;
14             }
15             if(isValid(s.substring(i-2, i))){
16                 third += first;
17             }
18             
19             first = second;
20             second = third;
21         }
22         return second;
23     }
24     
25     private boolean isValid(String s){
26         if(s.charAt(0) == '0'){
27             return false;
28         }
29         return Integer.valueOf(s) >= 1 && Integer.valueOf(s) <= 26;
30     }
31 }

跟上Decode Ways II.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4824951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值