-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeWays.java
More file actions
34 lines (31 loc) · 852 Bytes
/
Copy pathDecodeWays.java
File metadata and controls
34 lines (31 loc) · 852 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
public class DecodeWays {
public static void main(String[] args) {
String s = "1234";
System.out.println(s.substring(1, 2));
}
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int n = s.length();
int[] dp = new int[n + 1];
dp[n] = 1;
if (s.charAt(n - 1) == '0') {
dp[n - 1] = 0;
} else {
dp[n - 1] = 1;
}
for (int i = n - 2; i >= 0; i--) {
if (s.charAt(i) == '0') {
dp[i] = 0;
continue;
}
if ((s.charAt(i) - '0') * 10 + (s.charAt(i + 1) - '0') <= 26) {
dp[i] = dp[i + 1] + dp[i + 2];
} else {
dp[i] = dp[i + 1];
}
}
return dp[0];
}
}