-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (31 loc) · 955 Bytes
/
Solution.java
File metadata and controls
33 lines (31 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package leetcode._91_;
class Solution {
public int numDecodings(String s) { // 动态规划
if (s.startsWith("0")) {
return 0;
}
return this.numDecodings(s.toCharArray(), s.length(), new int[s.length()]);
}
private int numDecodings(char[] chars, int length, int[] dp) {
if (length <= 1) {
return 1;
}
if (dp[length - 1] > 0) {
return dp[length - 1];
}
int num = 0;
if (chars[length - 1] != '0') {
num += numDecodings(chars, length - 1, dp);
} else {
if (chars[length - 2] == '0') {
dp[length - 1] = num;
return num;
}
}
if (chars[length - 2] != '0' && (chars[length - 2] - '0') * 10 + chars[length - 1] - '0' <= 26) {
num += numDecodings(chars, length - 2, dp);
}
dp[length - 1] = num;
return num;
}
}