forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeWays.java
More file actions
24 lines (23 loc) · 748 Bytes
/
DecodeWays.java
File metadata and controls
24 lines (23 loc) · 748 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
/**
* TestCases
* ""
* "1"
* "1787897759966261825913315262377298132516969578441236833255596967132573482281598412163216914566534565"
* "7893749912342187894921836847319981199844151766195952528631828655978178193192959793156142441128167383"
*/
public class DecodeWays {
// DP,耗时1ms
public int numDecodings(String s) {
int len = s.length();
int[] dp = new int[len];
for (int i = 0; i < len; i++) {
if (s.charAt(i) != '0') {
dp[i] = i > 0 ? dp[i - 1] : 1;
}
if (i > 0 && (s.charAt(i - 1) == '1' || (s.charAt(i - 1) == '2' && s.charAt(i) <= '6'))) {
dp[i] += i > 1 ? dp[i - 2] : 1;
}
}
return dp[len - 1];
}
}