93. Restore IP Addresses
作者:
夜皇雪 | 来源:发表于
2016-11-27 15:25 被阅读0次public class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res=new ArrayList<>();
dfs(s,res,0,"",0);
return res;
}
private void dfs(String ip,List<String> res,int start,String s,int count){
if(count>4) return;
if(count==4&&start==ip.length()){
res.add(s);
return;
}
for(int i=1;i<4;i++){
if(start+i>ip.length()) break;
String temp=ip.substring(start,start+i);
if((temp.startsWith("0")&&temp.length()>1)||(i==3&&Integer.parseInt(temp)>=256)) continue;
dfs(ip,res,start+i,s+temp+(count==3?"":"."),count+1);
}
}
}
本文标题:93. Restore IP Addresses
本文链接:https://www.haomeiwen.com/subject/cnknpttx.html
网友评论