-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path91.decode-ways.java
More file actions
39 lines (33 loc) · 799 Bytes
/
91.decode-ways.java
File metadata and controls
39 lines (33 loc) · 799 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
35
36
37
38
39
/*
* @lc app=leetcode id=91 lang=java
*
* [91] Decode Ways
*/
// @lc code=start
class Solution {
// 2 1 0 1
// 1 2 1 1
// 1 0
// 1 1
public int numDecodings(String s) {
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
return 0;
}
int n = s.length();
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; ++i) {
int oneDigit = s.charAt(i - 1) - '0';
int twoDigits = Integer.parseInt(s.substring(i - 2, i));
if (oneDigit != 0) {
dp[i] += dp[i - 1];
}
if (10 <= twoDigits && twoDigits <= 26) {
dp[i] += dp[i - 2];
}
}
return dp[n];
}
}
// @lc code=end