-
Notifications
You must be signed in to change notification settings - Fork 0
/
Restore IP Address.java
33 lines (32 loc) · 1.14 KB
/
Restore IP Address.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
28
29
30
31
32
33
//http://blog.csdn.net/u013027996/article/details/23558309
public List<String> restoreIpAddresses(String s) {
List<String> iplist = new ArrayList<String>();
dfsSearch(s,0,new StringBuffer(),iplist);
return iplist;
}
public static void dfsSearch(String str,int part,StringBuffer ip,List<String> list){
if(part==4){
list.add(ip.toString());
return;
}
int len = str.length();
int min = Math.max(1,len-3*(3-part));
int max = Math.min(3,len-(3-part));
int cur = min;
while(cur>=min&&cur<=max){
String sub = str.substring(0,cur);
if(cur!=1&&sub.charAt(0)=='0'){
break;
}
int number = Integer.parseInt(sub);
if (number >= 0 && number < 256) {
StringBuffer subip = new StringBuffer(ip);
if (!"".equals(subip.toString())) {
subip.append(".");
}
subip.append(sub);
dfsSearch(str.substring(cur),part + 1,subip, list);
}
cur++;
}
}