-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRestoreIPAddresses.java
More file actions
29 lines (28 loc) · 992 Bytes
/
RestoreIPAddresses.java
File metadata and controls
29 lines (28 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class RestoreIPAddresses {
public List<String> restoreIpAddresses(String s) {
int len = s.length();
List<String> res = new ArrayList<>();
if (len < 4 || len > 12) return res;
List<String> comb = new ArrayList<>();
dfs(s, 0, res, comb);
return res;
}
public void dfs(String s, int start, List<String> res, List<String> comb) {
if (comb.size() == 4) {
if (start == s.length())
res.add(String.join(".", comb));
return;
}
for (int i = 1; i <= 3; i++) {
if (start + i <= s.length()) {
String seg = s.substring(start, start + i);
int n = Integer.valueOf(seg);
if (seg.equals("0") || (seg.charAt(0) != '0' && n <= 255)) {
comb.add(seg);
dfs(s, start + i, res, comb);
comb.remove(comb.size() - 1);
}
}
}
}
}