Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given "25525511135"
,
return ["255.255.11.135", "255.255.111.35"]
. (Order does not matter)
用pos[]放第i个点打的位置。然后搜索,当前节点合法,扩展节点的时候以当前的点的位置为基准,向右一位一位的增加,最多不超过3。
判断合法性要考虑每一节位数》1首位不能为0.
使用hashset判重。
boolean isValid(String s) {
if (s == null || s.length() == 0 || s.length() > 3)
return false;
if (s.length() > 1 && s.charAt(0) == '0')
return false;
Integer i = Integer.valueOf(s);
if (i >= 0 && i <= 255) return true;
return false;
}
void search(String s, int pos[], int cur, int n, int cnt) {
if (cur == n || cnt > 3) {
String ip1 = s.substring(0, pos[1] + 1)
.concat(".");
String ip2 = s.substring(pos[1] + 1, pos[2] + 1).concat(".");
String ip3 = s.substring(pos[2] + 1, pos[3] + 1).concat(".");
String ip4 = s.substring(pos[3] + 1);
if (isValid(ip4))
ips.add(ip1.concat(ip2).concat(ip3).concat(ip4));
return;
}
for (int i = 1; i <= 3; i++) {
if (cur + i >= n)
continue;
String str = s.substring(cur + 1, cur + i + 1);
if (isValid(str)) {
pos[cnt] = cur;
search(s, pos, cur + i, n, cnt + 1);
}
}
}
Set<String> ips = new HashSet<String>();
public List<String> restoreIpAddresses(String s) {
if (s == null || s.length() == 0 || s.length() > 12 || s.length() < 4)
return new ArrayList<String>();
int pos[] = new int[4];
search(s, pos, -1, s.length(), 0);
return new ArrayList<String>(ips);
}