[LeetCode][Java] 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.

题意:

一则包含字母A-Z 的信息被编码成数字:

‘A‘ -> 1
‘B‘ -> 2
...
‘Z‘ -> 26

给定一则包含数字的编码信息,判定出最终一共有多少种解码方式。

比如:

给定编码信息"12",它可以被解码成"AB" (12)或者"L" (12).

最终的解码方式为2.

算法分析:

参考博客: http://blog.csdn.net/u011095253/article/details/9248109

* 从头到尾扫这个String,比如我们想知道到,从第一位到dp[i]这一位组成的String,有多少种解码组合,那么有两种情况

* 第一:如果dp[i]所对应的的单个字符可以解码,那么dp[i]就包括前dp[i-1]位所积累的组合数 dp[i] = dp[i-1]

* 第二:如果不仅dp[i]所对应的的单个字符可以解码,dp[i-1] - dp[i],两个字符组成的也可以解码,

* 那么不仅包括dp[i-1]积累的组合数,也包括dp[i-2]位积累的组合数 dp[i] = dp[i-1] + dp[i-2]

* 我们建一个n+1的数组,为了程序简洁,我们在最前面放一个1。这样一来要注意数组里的index -1==String里的index

AC代码:

<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
    public int numDecodings(String s)
    {
        int n = s.length();
        if (n==0) return 0;
        int[] dp = new int[n+1];
        dp[0] = 1;
        if (isValid(s.substring(0,1))) dp[1] = 1;
        else dp[1] = 0;
        for(int i=2; i<=n;i++)
        {
            if (isValid(s.substring(i-1,i)))
                dp[i] = dp[i-1];
            if (isValid(s.substring(i-2,i)))
                dp[i] += dp[i-2];
        }
        return dp[n];
    }

    public boolean isValid(String s)
    {
        if (s.charAt(0)=='0') return false;
        int code = Integer.parseInt(s);
        return code>=1 && code<=26;
    }
}</span>

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-10-10 04:16:56

[LeetCode][Java] Decode Ways的相关文章

LeetCode之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

Leetcode 动态规划 Decode Ways

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Decode Ways Total Accepted: 8689 Total Submissions: 55465 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a

[LeetCode OJ] 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

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

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

[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 an encoded message containing digits, determine the total number of ways to decode it. For example, Given encode

leetcode 639 Decode Ways II

首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1]和s[i]decode,如果s[i-1]为*,那么**的组合共有15种(11,12,13.....,21,22..26),注意不是9*9=81种,因为我们是s[i-1]和s[i]一起decode,如果是38这样是大于26的 如果s[i-1]不为*,枚举s[i],组合起来小于26即可 if s[i]!

[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 an encoded message containing digits, determine the total number of ways to decode it. For example,Given enco

【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 num

[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 an encoded message containing digits, determine the total number of ways to decode it. For example,Given encoded