-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodeWays.java
70 lines (68 loc) · 2.1 KB
/
DecodeWays.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class Solution {
public int numDecodings(String s) {
if (s.length() == 0)
return 0;
int[] memo = new int[s.length()];
return numDecodings(s, 0, memo);
}
public int numDecodingsDP(String s) {
int len = s.length();
if (len == 0)
return 0;
if (s.charAt(0) == '0')
return 0;
int[] dp = new int[len+1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= len; i++) {
char i_2 = s.charAt(i - 2);
char i_1 = s.charAt(i - 1);
if (i_2 == '1') {
if (i_1 == '0') dp[i] = dp[i-2];
else dp[i] = dp[i-1] + dp[i-2];
}
else if (i_2 == '2') {
if (i_1 == '0') dp[i] = dp[i-2];
else if (i_1 > '6') dp[i] = dp[i-1];
else dp[i] = dp[i-1] + dp[i-2];
}
else {
if (i_1 == '0') return 0;
else dp[i] = dp[i-1];
}
}
return dp[len];
}
private int numDecodings(String s, int start, int[] memo) {
int len = s.length();
if (start >= len)
return 1;
if (memo[start] > 0)
return memo[start];
char first = s.charAt(start);
if (first == '0')
return 0;
if (start == len - 1)
return 1;
int ret = 0;
char second = s.charAt(start + 1);
if (first == '1') {
if (second == '0')
ret = numDecodings(s, start + 2, memo);
else
ret = numDecodings(s, start + 1, memo) + numDecodings(s, start + 2, memo);
}
else if (first == '2') {
if (second == '0')
ret = numDecodings(s, start + 2, memo);
else if (second <= '6')
ret = numDecodings(s, start + 1, memo) + numDecodings(s, start + 2, memo);
else
ret = numDecodings(s, start + 2, memo);
}
else
ret = numDecodings(s, start + 1, memo);
memo[start] = ret;
return ret;
}
}