Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b161c02

Browse files
Add Solution.java to problems 0091
1 parent 9b59fa9 commit b161c02

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int numDecodings(String s) {
3+
if (s.charAt(0) == '0') return 0;
4+
5+
char[] chars = s.toCharArray();
6+
return decode(chars, chars.length - 1);
7+
}
8+
9+
int decode(char[] chars, int index) {
10+
if (index <= 0) return 1;
11+
12+
int count = 0;
13+
char curr = chars[index];
14+
char prev = chars[index - 1];
15+
16+
// 当前字符比 "0" 大,则直接利用它之前的字符串所求得的结果
17+
if (curr > '0') {
18+
count = decode(chars, index - 1);
19+
}
20+
// 由前一个字符和当前字符所构成的数字,值必须要在 1 到 26 之间,否则无法进行解码
21+
if (prev == '1' || (prev == '2' && curr <= '6')) {
22+
count += decode(chars, index - 2);
23+
}
24+
return count;
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /