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

Commit0e5ae7d

Browse files
committed
restore
1 parente9160df commit0e5ae7d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

‎string/RestoreIpAddresses.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
packageAlgorithms.string;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.List;
5+
6+
publicclassRestoreIpAddresses {
7+
publicList<String>restoreIpAddresses(Strings) {
8+
if (s ==null) {
9+
returnnull;
10+
}
11+
12+
ArrayList<String>ret =newArrayList<String>();
13+
ArrayList<String>path =newArrayList<String>();
14+
15+
dfs(s,0,path,ret);
16+
17+
returnret;
18+
}
19+
20+
publicvoiddfs(Strings,intindex,ArrayList<String>path,ArrayList<String>ret) {
21+
if (path.size() ==4) {
22+
if (index ==s.length() &&path.size() ==4) {
23+
StringBuildersb =newStringBuilder();
24+
for (Stringstr:path) {
25+
sb.append(str);
26+
sb.append('.');
27+
}
28+
29+
sb.deleteCharAt(sb.length() -1);
30+
ret.add(sb.toString());
31+
}
32+
33+
return;
34+
}
35+
36+
intlen =s.length();
37+
for (inti =index;i <index +3 &&i <len;i++) {
38+
if (s.charAt(index) =='0' &&i >index) {
39+
break;
40+
}
41+
42+
Stringpre =s.substring(index,i +1);
43+
// 过滤Number > 255的情况。
44+
intnum =Integer.parseInt(pre);
45+
if (num >255) {
46+
continue;
47+
}
48+
49+
path.add(pre);
50+
dfs(s,i +1,path,ret);
51+
path.remove(path.size() -1);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp