Skip to content

Commit 8a1c8c4

Browse files
committed
add solution of decode-ways
1 parent 30b47d2 commit 8a1c8c4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

decode-ways/jinhyungrhee.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
class Solution {
3+
4+
public List<Integer> nums;
5+
public int numDecodings(String s) {
6+
nums = new ArrayList<>();
7+
for (int i = 0; i < s.length(); i++) {
8+
nums.add(s.charAt(i) - 48);
9+
}
10+
11+
int[] memo = new int[nums.size() + 1];
12+
Arrays.fill(memo, -1);
13+
return dfs(0, memo);
14+
}
15+
16+
public int dfs(int start, int[] memo) {
17+
18+
if (start == nums.size()) return 1;
19+
20+
if (memo[start] != -1) {
21+
return memo[start];
22+
}
23+
if (nums.get(start) == 0) {
24+
memo[start] = 0;
25+
}
26+
else if (start + 1 < nums.size() && nums.get(start) * 10 + nums.get(start + 1) < 27) {
27+
memo[start] = dfs(start + 1, memo) + dfs(start + 2, memo);
28+
} else {
29+
memo[start] = dfs(start + 1, memo);
30+
}
31+
return memo[start];
32+
}
33+
}

0 commit comments

Comments
 (0)