Skip to content

Commit 00286ad

Browse files
committed
0091. Decode Ways
1 parent 2ba50b8 commit 00286ad

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

markdown/0091. Decode Ways.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
### [91\. Decode Ways](https://leetcode.com/problems/decode-ways/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
A message containing letters from `A-Z` is being encoded to numbers using the following mapping:
7+
8+
```
9+
'A' -> 1
10+
'B' -> 2
11+
...
12+
'Z' -> 26
13+
```
14+
15+
Given a **non-empty** string containing only digits, determine the total number of ways to decode it.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: "12"
21+
Output: 2
22+
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: "226"
29+
Output: 3
30+
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
31+
```
32+
33+
34+
#### Solution A (推荐)
35+
36+
Language: **Java**
37+
38+
```java
39+
class Solution {
40+
   public int numDecodings(String s) { // 动态规划
41+
       if (s == null || s.length() == 0) {
42+
           return 0;
43+
      }
44+
       int[] dp = new int[s.length() + 1];
45+
       dp[0] = 1;
46+
       dp[1] = s.charAt(0) != '0' ? 1 : 0;
47+
       for (int i = 2; i <= s.length(); i++) {
48+
           int first = Integer.parseInt(s.substring(i - 1, i));
49+
           int second = Integer.parseInt(s.substring(i - 2, i));
50+
           if (first > 0 && first < 10) {
51+
               dp[i] += dp[i - 1];
52+
          }
53+
           if (second >= 10 && second <= 26) {
54+
               dp[i] += dp[i - 2];
55+
          }
56+
      }
57+
       return dp[s.length()];
58+
  }
59+
}
60+
```
61+
62+
63+
#### Solution B
64+
65+
Language: **Java**
66+
67+
```java
68+
class Solution {
69+
public int numDecodings(String s) { // 动态规划
70+
if (s.startsWith("0")) {
71+
return 0;
72+
}
73+
return this.numDecodings(s.toCharArray(), s.length(), new int[s.length()]);
74+
}
75+
76+
private int numDecodings(char[] chars, int length, int[] dp) {
77+
if (length <= 1) {
78+
return 1;
79+
}
80+
if (dp[length - 1] > 0) {
81+
return dp[length - 1];
82+
}
83+
int num = 0;
84+
if (chars[length - 1] != '0') {
85+
num += numDecodings(chars, length - 1, dp);
86+
} else {
87+
if (chars[length - 2] == '0') {
88+
dp[length - 1] = num;
89+
return num;
90+
}
91+
}
92+
if (chars[length - 2] != '0' && (chars[length - 2] - '0') * 10 + chars[length - 1] - '0' <= 26) {
93+
num += numDecodings(chars, length - 2, dp);
94+
}
95+
dp[length - 1] = num;
96+
return num;
97+
}
98+
}
99+
```
100+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-03-tX1fHU.jpg)

src/main/java/leetcode/_91_/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._91_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution2 solution = new Solution2();
9+
System.out.println( solution.numDecodings("101"));
10+
}
11+
}
12+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode._91_;
2+
3+
class Solution {
4+
public int numDecodings(String s) { // 动态规划
5+
if (s.startsWith("0")) {
6+
return 0;
7+
}
8+
return this.numDecodings(s.toCharArray(), s.length(), new int[s.length()]);
9+
}
10+
11+
private int numDecodings(char[] chars, int length, int[] dp) {
12+
if (length <= 1) {
13+
return 1;
14+
}
15+
if (dp[length - 1] > 0) {
16+
return dp[length - 1];
17+
}
18+
int num = 0;
19+
if (chars[length - 1] != '0') {
20+
num += numDecodings(chars, length - 1, dp);
21+
} else {
22+
if (chars[length - 2] == '0') {
23+
dp[length - 1] = num;
24+
return num;
25+
}
26+
}
27+
if (chars[length - 2] != '0' && (chars[length - 2] - '0') * 10 + chars[length - 1] - '0' <= 26) {
28+
num += numDecodings(chars, length - 2, dp);
29+
}
30+
dp[length - 1] = num;
31+
return num;
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode._91_;
2+
3+
class Solution2 {
4+
public int numDecodings(String s) { // 动态规划
5+
if (s == null || s.length() == 0) {
6+
return 0;
7+
}
8+
int[] dp = new int[s.length() + 1];
9+
dp[0] = 1;
10+
dp[1] = s.charAt(0) != '0' ? 1 : 0;
11+
for (int i = 2; i <= s.length(); i++) {
12+
int first = Integer.parseInt(s.substring(i - 1, i));
13+
int second = Integer.parseInt(s.substring(i - 2, i));
14+
if (first > 0 && first < 10) {
15+
dp[i] += dp[i - 1];
16+
}
17+
if (second >= 10 && second <= 26) {
18+
dp[i] += dp[i - 2];
19+
}
20+
}
21+
return dp[s.length()];
22+
}
23+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
### [91\. Decode Ways](https://leetcode.com/problems/decode-ways/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
A message containing letters from `A-Z` is being encoded to numbers using the following mapping:
7+
8+
```
9+
'A' -> 1
10+
'B' -> 2
11+
...
12+
'Z' -> 26
13+
```
14+
15+
Given a **non-empty** string containing only digits, determine the total number of ways to decode it.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: "12"
21+
Output: 2
22+
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: "226"
29+
Output: 3
30+
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
31+
```
32+
33+
34+
#### Solution A (推荐)
35+
36+
Language: **Java**
37+
38+
```java
39+
class Solution {
40+
   public int numDecodings(String s) { // 动态规划
41+
       if (s == null || s.length() == 0) {
42+
           return 0;
43+
      }
44+
       int[] dp = new int[s.length() + 1];
45+
       dp[0] = 1;
46+
       dp[1] = s.charAt(0) != '0' ? 1 : 0;
47+
       for (int i = 2; i <= s.length(); i++) {
48+
           int first = Integer.parseInt(s.substring(i - 1, i));
49+
           int second = Integer.parseInt(s.substring(i - 2, i));
50+
           if (first > 0 && first < 10) {
51+
               dp[i] += dp[i - 1];
52+
          }
53+
           if (second >= 10 && second <= 26) {
54+
               dp[i] += dp[i - 2];
55+
          }
56+
      }
57+
       return dp[s.length()];
58+
  }
59+
}
60+
```
61+
62+
63+
#### Solution B
64+
65+
Language: **Java**
66+
67+
```java
68+
class Solution {
69+
public int numDecodings(String s) { // 动态规划
70+
if (s.startsWith("0")) {
71+
return 0;
72+
}
73+
return this.numDecodings(s.toCharArray(), s.length(), new int[s.length()]);
74+
}
75+
76+
private int numDecodings(char[] chars, int length, int[] dp) {
77+
if (length <= 1) {
78+
return 1;
79+
}
80+
if (dp[length - 1] > 0) {
81+
return dp[length - 1];
82+
}
83+
int num = 0;
84+
if (chars[length - 1] != '0') {
85+
num += numDecodings(chars, length - 1, dp);
86+
} else {
87+
if (chars[length - 2] == '0') {
88+
dp[length - 1] = num;
89+
return num;
90+
}
91+
}
92+
if (chars[length - 2] != '0' && (chars[length - 2] - '0') * 10 + chars[length - 1] - '0' <= 26) {
93+
num += numDecodings(chars, length - 2, dp);
94+
}
95+
dp[length - 1] = num;
96+
return num;
97+
}
98+
}
99+
```
100+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-03-tX1fHU.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy