要实现一个重组网址的功能,把可能出现的情况给穷举出来,下面代码已经实现(写的比较烂,请见谅),但是在网址比较长的情况下,重组网址的时候遇到大循环问题,导致内存溢出,请各位大侠给优化一下
public static void main(String [] args) {
dealUrl("www。qΒ5。c Om");
}
private static void dealUrl(String url) {
char[] ac = url.toLowerCase().toCharArray();
List<List<String>> charList = new ArrayList<List<String>>();
List<String> tempList = null;
for (int i = 0; i < ac.length; i++) {
tempList = new ArrayList<String>();
int charIntValue = (int) ac[i];
if (Character.isDigit(ac[i])) {//数字
tempList.add(ac[i]+"");
} else if (charIntValue == 46 ||charIntValue == 65294 ){ //点
tempList.add(((char) 46)+"");//半角点
tempList.add(((char) 65294)+"");//全角点
tempList.add("。");//句号
} else if (charIntValue == 79 || charIntValue == 65359
|| charIntValue == 111 || charIntValue == 65327){//字母o
tempList.add(((char) 79)+"");//半角o
tempList.add(((char) 65359)+"");//全角o
tempList.add(((char) 111)+"");//半角大写o
tempList.add(((char) 65327)+"");//全角大写o
tempList.add("0");//零
} else if(charIntValue == 12288 || charIntValue == 32) {//空格
tempList.add(((char) 32)+"");//半角空格
tempList.add(((char) 12288)+"");//全角空格
} else if (charIntValue >= 65281 && charIntValue <= 65374) {//全角
char n = (char) (charIntValue - 65248);
tempList.add(ac[i]+"");//全角字符
tempList.add(n+"");//半角字符
tempList.add((ac[i]+"").toUpperCase());//全角大写
tempList.add((n+"").toUpperCase());//半角大写
} else {
char n = (char) (65248+charIntValue);
tempList.add(ac[i]+"");//半角字符
tempList.add(n+"");//全角字符
tempList.add((ac[i]+"").toUpperCase());//半角大写
tempList.add((n+"").toUpperCase());//全角大写
}
charList.add(tempList);
}
//分组处理
List<List<String>> partChongZuList = new ArrayList<List<String>>();
List<List<String>> partList = null;
int allSize = charList.size();
int fenGe = 4;
int partCount = allSize/fenGe;
if (partCount > 0 ) {
for (int i = 0; i < allSize/fenGe; i++) {
int tempSize = fenGe*(i+1);
if(allSize/tempSize == 1) {
tempSize = allSize;
}
partList = new ArrayList<List<String>>();
for (int k = i*fenGe; k < tempSize; k++) {
partList.add(charList.get(k));
}
partChongZuList .add(chongZu(partList));
}
List<String> aa = chongZu(partChongZuList);
System.out.println(aa.size());
} else {
List<String> aa = chongZu(charList);
System.out.println(aa.size());
}
}
/**
* 重组网址
* @param charList
*/
private static List<String> chongZu(List<List<String>> charList){
List<String> newStr = new ArrayList<String>();
List<String> tempStr = new ArrayList<String>();
int allLn = charList.size();
for (int i = 0; i < allLn; i++) {
List<String> t = charList.get(i);
int tSize = t.size();
for (int j = 0; j < tSize; j++) {
if (i == 0) {
tempStr.add( t.get(j));
} else {
String ns = "";
int nSize = newStr.size();
for (int k = 0 ; k < nSize; k++) {
ns = newStr.get(k);
ns += t.get(j);
tempStr.add(ns);
}
}
}
newStr.clear();
newStr.addAll(tempStr);
tempStr.clear();
System.out.println(newStr.size());
}
return newStr;
}