import java.util.*;
public class Main {
public static void main(String[] args) {
String ipAdress = new String("19216801");
char[] ip = ipAdress.toCharArray();
dfs(ip,0,new String[4],0);
}
public static void dfs(char[] arr,int level,String[] str,int used){
if(level>=4 || used==arr.length){
if(level>=4 && used==arr.length) {
System.out.println(Arrays.toString(str));
}
return;
}
for(int i=1;i<4;i++){ //取i个数
String temp = "";
int len = used+i;
if(len>arr.length){ //防止越界
len=arr.length;
}
for(int j=used;j<len;j++){ //给存储结果赋值
temp = temp+arr[j];
}
if(temp.length()>1 && temp.substring(0,1).equals("0")){ //防止出现01这种现象
continue;
}
str[level] = temp;
if(Integer.valueOf(str[level])<=255&&Integer.valueOf(str[level])>=0){ //筛选
dfs(arr,level+1,str,used+i);
}
str[level] = ""; //复原
}
}
}
给一个字符串切割为ip
最新推荐文章于 2025-03-04 10:04:03 发布
133

被折叠的 条评论
为什么被折叠?



