LeetCode 91. Decode Ways 解码方式(Java)

题目:

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

Example 2:
Input: “226”
Output: 3
Explanation: It could be decoded as “BZ” (2 26), “VF” (22 6), or “BBF” (2 2 6).

解答:

这是一道动态规划的题,可惜开始拿到题目时并没有准确的思路。

假设给定字符串“1226”,则

当前位置		编码方式
  [0]		1
  [1]		1-2			12
  [2]		1-2-2		12-2		1-22
  [3]		1-2-2-6		12-2-6		1-22-6		1-2-26		12-26	

由上述我们可以看出,假如在位置[3]时,可能的情况为两种:
一种是在位置[2]的所有情况中,后面均加上字符‘6’
第二种情况是在位置[1]的所有情况中,后面均加上‘26’

因此可以分析得出,在考虑第 i 个加进来的字符时:(用dp[i]表示前i个字符的解码方式)

  1. 若当前字符不为0,自己可以构成一个单独信息,则 dp[i] = dp[i-1]
  2. 若当前字符可与上一字符进行合并,构成有效信息,即上一字符不为0,且合并后得到的数字 <= 26,则 dp[i] = dp[i-2]
  3. 若上述两种情况均满足,则 dp[i]=dp[i-1]+dp[i-2]

代码实现如下:

class Solution {
    public int numDecodings(String s) {
        if(s.length() == 0) {
            return 0;
        }
        int[] dp = new int[s.length() + 1];
        //防止之后遍历时,i=1时dp[i-1]越界。dp[i]恰好表示第i个字符的编码方式
        dp[0] = 1;
        if(s.charAt(0) == '0') {
            return 0;
        } else {
            dp[1] = 1;
        }
        for(int i=1; i<s.length(); i++) {
        	//若当前字符不为0,则可单独构成有效信息
            if(s.charAt(i) != '0' ) {
                dp[i+1] += dp [i];
            }
            int twoDigits = (s.charAt(i-1)-'0')*10 + (s.charAt(i)-'0');
            //注意此处要判断上一位不为0的情况,否则会出现03这样的无效信息
            if(s.charAt(i-1) != '0' && twoDigits <= 26) {
                dp[i+1] += dp[i-1];
            }
        }
        return dp[s.length()];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值