BiruLyu
8/2/2017 - 10:04 PM

``````public class Solution {
List<String> ret=new ArrayList<>();
int num=0;

if(s.length()<4||s.length()>12)return ret;

char[] array=s.toCharArray();
char[] path=new char[s.length()+3];

for(int i=0;i<3;i++){

num=num*10+array[i]-'0';
if(num>255) break;
path[i]=array[i];
helper(array,path,i+1,i+1,0,ret);

if(num==0) break;

}

return ret;

}
private void helper(char [] array, char[] path, int length, int idx, int count, List<String> ret){

if(count==3){
if(idx==array.length){
}

return ;
}

path[length]='.';
int num=0;
for(int i=idx;i<Math.min(idx+3,array.length);i++){

num=num*10+array[i]-'0';
if(num>255) break;

path[length+1+(i-idx)]=array[i];

helper(array,path,length+1+(i-idx)+1,i+1,count+1,ret);

if(num==0){
break;
}

}

}

}``````
``````public class Solution {
List<String> res = new ArrayList<String>();
int len = s.length();
for(int i = 1; i<4 && i<len-2; i++){
for(int j = i+1; j<i+4 && j<len-1; j++){
for(int k = j+1; k<j+4 && k<len; k++){
String s1 = s.substring(0,i), s2 = s.substring(i,j), s3 = s.substring(j,k), s4 = s.substring(k,len);
if(isValid(s1) && isValid(s2) && isValid(s3) && isValid(s4)){