-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumDecodingObj.java
More file actions
34 lines (29 loc) · 913 Bytes
/
Copy pathNumDecodingObj.java
File metadata and controls
34 lines (29 loc) · 913 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
34
package test20201018;
public class NumDecodingObj {
public int numDecodings(String s) {
int len = s.length();
if (len == 0) {
return 0;
}
// dp[i] 以 s[i - 1] 结尾的前缀子串有多少种解法方法
// dp[i] = dp[i - 1] * 1 if nums[i - 1] != '0'
// dp[i] += dp[i - 2] * 1 if 10 <= int(s[i - 2..i - 1]) <= 26
int[] dp = new int[len + 1];
dp[0] = 1;
char[] charArray = s.toCharArray();
if (charArray[0] == '0') {
return 0;
}
dp[1] = 1;
for (int i = 1; i < len; i++) {
if (charArray[i] != '0') {
dp[i + 1] = dp[i];
}
int num = 10 * (charArray[i - 1] - '0') + (charArray[i] - '0');
if (num >= 10 && num <= 26) {
dp[i + 1] += dp[i - 1];
}
}
return dp[len];
}
}