public class {
List<String> res = new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
dfs(s, "", 0, 0);
return res;
}
private void dfs(String s, String cur, int index,int count) {
if(index == s.length() && count == 4){
res.add(cur.substring(0, cur.length()-1));
}
if(count > 4 || index== s.length() ) {
return;
}
if(s.charAt(index) == '0') {
dfs(s, cur+"0"+".", ++index,count+1);
} else {
String num = s.charAt(index) + "";
while(index<s.length() && Integer.parseInt(num) <= 255) {
dfs(s, cur+num+".", ++index, count+1);
if(index==s.length()){
break;
}
num = num+s.charAt(index);
}
}
}
}
近期评论