Movatterモバイル変換


[0]ホーム

URL:


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

Commit589ac08

Browse files
committed
decode.
1 parentefd6874 commit589ac08

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎dp/NumDecodings.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
packageAlgorithms.dp;
2+
3+
publicclassNumDecodings {
4+
publicintnumDecodings(Strings) {
5+
if (s ==null ||s.length() ==0) {
6+
return0;
7+
}
8+
9+
intlen =s.length();
10+
11+
// D[i] 表示含有i个字符的子串的DECODE WAYS.
12+
int[]D =newint[len +1];
13+
14+
D[0] =1;
15+
16+
for (inti =1;i <=len;i++) {
17+
D[i] =0;
18+
19+
// 现在正在考察的字符的索引.
20+
intindex =i -1;
21+
// 最后一个字符独立解码
22+
if (isValidSingle(s.charAt(index))) {
23+
D[i] +=D[i -1];
24+
}
25+
26+
// 最后一个字符与上一个字符一起解码
27+
if (i >1 &&isValidTwo(s.substring(index -1,index +1))) {
28+
D[i] +=D[i -2];
29+
}
30+
}
31+
32+
returnD[len];
33+
}
34+
35+
publicbooleanisValidSingle(charc) {
36+
if (c >='1' &&c <='9') {
37+
returntrue;
38+
}
39+
40+
returnfalse;
41+
}
42+
43+
publicbooleanisValidTwo(Strings) {
44+
intnum =Integer.parseInt(s);
45+
46+
return (num >=10 &&num <=26);
47+
}
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp