-
Notifications
You must be signed in to change notification settings - Fork 2
/
RestoreIP.java
27 lines (24 loc) · 914 Bytes
/
RestoreIP.java
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
package leetcode.string;
import java.util.ArrayList;
import java.util.List;
public class RestoreIP {
public List<String> restoreIpAddresses(String s) {
List<String> result = new ArrayList<>();
restoreIP("", s, 0, result);
return result;
}
private void restoreIP(String addr, String remain, int currentLevel, List<String> result) {
if (remain.isEmpty() && currentLevel == 4) {
result.add(addr.substring(1));
} else if (remain.isEmpty() || currentLevel == 4) {
return;
} else {
for (int i = 1; i <= remain.length() && i <= (remain.charAt(0) == 48 ? 1 : 3); i++) {
String part = remain.substring(0, i);
if (Integer.parseInt(part) < 256) {
restoreIP(addr + "." + part, remain.substring(i), currentLevel + 1, result);
}
}
}
}
}