-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeWays.java
More file actions
53 lines (47 loc) · 1.55 KB
/
DecodeWays.java
File metadata and controls
53 lines (47 loc) · 1.55 KB
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
package leetcode;
/**
* DecodeWays
* https://leetcode-cn.com/problems/decode-ways/
* 91. 解码方法
* https://leetcode-cn.com/problems/decode-ways/solution/dong-tai-gui-hua-fei-bo-na-qi-by-oshdyr-zq0u/
*
* @since 2021-04-21
*/
public class DecodeWays {
public static void main(String[] args) {
DecodeWays sol = new DecodeWays();
System.out.println(sol.numDecodings("1"));
System.out.println(sol.numDecodings("226"));
System.out.println(sol.numDecodings("2206"));
System.out.println(sol.numDecodings("0"));
System.out.println(sol.numDecodings("06"));
System.out.println(sol.numDecodings("1044"));
}
public int numDecodings(String s) {
int[] counts = new int[s.length()];
int lastDigit = 0;
for (int i = 0; i < s.length(); i++) {
int currDigit = s.charAt(i) - '0';
int oneCount = 0;
if (currDigit > 0 && currDigit < 10) {
if (i > 0) {
oneCount = counts[i - 1];
} else {
oneCount = 1;
}
}
int twoCount = 0;
int value = lastDigit * 10 + currDigit;
if (lastDigit > 0 && value > 0 && value < 27 && i > 0) {
if (i > 1) {
twoCount = counts[i - 2];
} else {
twoCount = 1;
}
}
lastDigit = currDigit;
counts[i] = oneCount + twoCount;
}
return counts[s.length() - 1];
}
}