Jan 23 - Decode Ways; DP; Array;

public class Solution {
    public int numDecodings(String s) {
        int len = s.length();
        if(len == 0) return 0;
        int[] counts = new int[len];
        counts[0] = 0;
        for(int i = 0; i < len; i++){
            int flag = 0;
            if(i == 0) {
                if(s.charAt(i) != ‘0‘) counts[i] = 1;
                else counts[i] = 0;
                continue;
            }
            if(i == 1){
                int mes = Integer.parseInt(s.charAt(i-1)+""+s.charAt(i));
                if(mes >= 10 && mes <= 26) {
                   flag++;
                }
                if(s.charAt(i) != ‘0‘) flag++;
                if(flag == 2) counts[i] = counts[i-1]+1;
                if(flag == 1) counts[i] = counts[i-1];
                if(flag == 0) counts[i] = 0;
                continue;
            }

                int mes = Integer.parseInt(s.charAt(i-1)+""+s.charAt(i));
                if(mes >= 10 && mes <= 26) {
                    flag++;
                    counts[i] = counts[i-2];
                }
                if(s.charAt(i) != ‘0‘) {
                    flag++;
                    counts[i] = counts[i-1];
                }
                if(flag == 2) counts[i] = counts[i-2]+counts[i-1];

        }

        return counts[len-1];
    }
}
时间: 2024-10-07 01:29:49

Jan 23 - Decode Ways; DP; Array;的相关文章

【Leetcode】Decode Ways (DP)

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

Jan 23 - Search In Rotated Array II; Array; Binary Search;

public class Solution { public boolean search(int[] nums, int target) { int len = nums.length; if(len == 0) return false; int low = 0; int high = len-1; while(low < high){ int mid = (low+high)/2; // do not concern the integer overrange situation if(n

[LeetCode] Decode Ways(DP)

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

[Swift]LeetCode91. 解码方法 | 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: &qu

[Swift]LeetCode639. 解码方法 2 | Decode Ways II

A message containing letters from A-Z is being encoded to numbers using the following mapping way: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Beyond that, now the encoded string can also contain the character '*', which can be treated as one of the numbers

Decode Ways leetcode 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 enc

LeetCode:Decode Ways 解题报告

Decode WaysA message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given e

【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 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]!