2
头图

Decoding method

Title description: A message containing the letters AZ is encoded by the following mapping:

'A' -> 1
'B' -> 2
...
'Z' -> 26
To decode the encoded message, all numbers must be mapped back to letters based on the above mapping method (there may be multiple methods). For example, "11106" can be mapped to:

"AAJF", group the message as (1 1 10 6)
"KJF", group the message as (11 10 6)
Note that the message cannot be grouped as (1 11 06) because "06" cannot be mapped to "F". This is because "6" and "06" are not equivalent in the mapping.

Give you a non-empty string s containing only numbers, please count and return the total number of decoding methods.

The question data guarantees that the answer must be a 32-bit integer.

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/decode-ways/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution 1: Recursive exhaustion
  • First of all, when s is null or an empty string or s is a string starting 0 0
  • If s is 1, return 1 directly.
  • Then a recursive process when S if the length is greater than 1, processing logic follows the recursive method (the method of Reference left and right start and end positions, respectively, to match the current character 0 < (right - left) < 3 ):

    • If left is 0, that is, the character to be matched starts with 0, it cannot be mapped, and it returns directly;
    • If the left and right is greater than 26, it cannot be mapped, return;
    • If right is the last digit s result plus 1 and return;
    • If right is s , and the last digit is not 0, then result plus 1 and return;
    • Later, we will continue to recursively process right ~ right + 1 and right ~ right + 2 according to the number of digits after right.
  • Finally, the result returned is the total number of decoding methods.
public class LeetCode_091 {

    private static int result = 0;

    /**
     * 递归 穷举:性能较差,提交会超时
     *
     * @param s
     * @return
     */
    public static int numDecodings(String s) {
        // 这些情况无法映射,直接返回0
        if (s == null || s == "" || s.equals("0") || s.startsWith("0")) {
            return 0;
        }

        if (s.length() == 1) {
            return 1;
        }

        numDecodings(s, 0, 1);
        numDecodings(s, 0, 2);
        return result;
    }



    public static void numDecodings(String s, int left, int right) {
        if (s.charAt(left) == '0') {
            return;
        }
        if (Integer.valueOf(s.substring(left, right)) > 26) {
            return;
        }
        if (s.length() - right == 0) {
            result++;
            return;
        }
        if (s.length() - right == 1 && s.charAt(s.length() - 1) != '0') {
            result++;
            return;
        }
        numDecodings(s, right, right + 1);
        if (s.length() - right > 1) {
            numDecodings(s, right, right + 2);
        }
    }

    public static void main(String[] args) {
        System.out.println(numDecodings("226"));
    }
}
[Daily Message] with the sky is endless fun! Fighting with the land is endless joy! Fighting with others is endless fun!

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!