Skip to content

Commit 756ef83

Browse files
committed
decode ways solved
1 parent 262833a commit 756ef83

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

decode-ways/sora0319.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public int numDecodings(String s) {
3+
if (s == null || s.length() == 0 || s.charAt(0) == '0') return 0;
4+
5+
int n = s.length();
6+
int[] dp = new int[n + 1];
7+
dp[0] = 1;
8+
dp[1] = 1;
9+
10+
for (int i = 2; i <= n; i++) {
11+
if (s.charAt(i - 1) != '0') {
12+
dp[i] += dp[i - 1];
13+
}
14+
15+
int twoNum = Integer.parseInt(s.substring(i - 2, i));
16+
if (twoNum >= 10 && twoNum <= 26) {
17+
dp[i] += dp[i - 2];
18+
}
19+
}
20+
21+
return dp[n];
22+
}
23+
}
24+

0 commit comments

Comments
 (0)