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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- LeetCode 93 Restore IP Addresses Given a string containin...
- Given a string containing only digits, restore it by retu...
- Given a string containing only digits, restore it by retu...
- NAIVE 解法,O(n3): dfs dfs解法我写了一个,我想的是类似Combination Sum那种的;但...