判断字符串有无重复字符之开辟额外数组
public class 判断字符串有无重复字符 {
public static void main(String[] args) {
String str="afdsgkl";
String str1="aadfsf";
System.out.println(cheack(str));
System.out.println(cheack(str1));
}
public static boolean cheack(String str) {
// TODO Auto-generated method stub
int []flag=new int[128];//开辟空间
//字符串扫描
for(int i=0;i<str.length();i++) {
int c=(int)str.charAt(i);//遍历的字符串转为Int
if(flag[c]>0) {//0>0
return false;
}else {
flag[c]++;//索引的字符串下标加1 表示已经有了
}
}
return true;
}
}