给定一个字符串string iniString,请返回一个bool值,True代表字符串的所有字符全都不同,False代表存在相同的字符。保证字符串中的字符为ASCII字符且不允许使用额外的存储结构,字符串的长度小于等于3000。大小写字母算不同的字符
"aeiou"
返回:True
"BarackObama"
返回:False
import java.util.*; public class Different { public boolean checkDifferent(String iniString) { // write code here Set<Character> set = new HashSet(); char[] a = iniString.toCharArray(); for(int i = 0; i < a.length; i++){ if(!set.add(a[i])) return false; } return true; } }
public class Different { public boolean checkDifferent(String iniString) { for(int i=0;i<iniString.length();i++){ String s=iniString.substring(i,i+1); int index=iniString.lastIndexOf(s); if(index!=i){ return false; } } return true; } }
import java.util.*; public class Different { public boolean checkDifferent(String iniString) { if(iniString.length() > 3000) return false; boolean[] char_set = new boolean[256]; for(int i = 0;i < iniString.length();i++) { int val = iniString.charAt(i); if(val > 256 ||char_set[val] == true){// return false; } char_set[val] = true; } return true; } }
import java.util.*; //利用了直接插入的思想比较 public class Different { public boolean checkDifferent(String iniString) { //将字符串转换位字符数组 char[] c = iniString.toCharArray(); boolean flag = true; for(int i=1; i<iniString.length(); i++){ if (flag == false) break; char temp = c[i]; for(int j=i-1; j>=0; j--){ if(c[j] == temp){ flag = false; break; } } } return flag; } public static void main(String[] args) { Different test = new Different(); String s1 = "aeiou"; System.out.println(test.checkDifferent(s1)); String s2 = "BarackObama"; System.out.println(test.checkDifferent(s2)); } }
public boolean checkDifferent(String iniString) { // write code here char c=(char) 0; int length=iniString.length(); for(int i=0;i<256;i++){ boolean flag=false; for(int j=0;j<length;j++){ if(c==iniString.charAt(j)){ if(flag){ return false; }else{ flag=true; } } } c++; } return true; }
//两层循环暴力搜素,有相同的字符就返回false,否则返回true,时间复杂度O(n^2) public class Different { public boolean checkDifferent(String iniString) { // write code here for (int i = 0; i < iniString.length() - 1; i++) { for (int j = i + 1; j < iniString.length(); j++) { if (iniString.charAt(i) == iniString.charAt(j)) { return false; } } } return true; } }
import java.util.*; public class Different { public boolean checkDifferent(String iniString) { for(int i=0;i<iniString.length();i++){ for(int j=i+1;j<iniString.length();j++){ if (iniString.charAt(j)==iniString.charAt(i)) { return false; } } } return true; // write code here } }
public static void main(String[] args) {
String str = "abdcxc";
toAsscii(str);
}
public static void toAsscii(String str) {
char[] c = str.toCharArray();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < c.length; i++) {
list.add((int) c[i]);
}
Set<Integer> set = new HashSet<>(list);
if(list.size() == set.size()) {
System.out.println(true);
}else {
System.out.println(false);
}
}
public class Different { public boolean checkDifferent(String iniString) { //先排序,然后检查相邻两个字符 char[] iniChars = iniString.toCharArray(); Arrays.sort(iniChars); int j = 0; for (int i=1; i<iniChars.length; i++){ if (iniChars[i] == iniChars[j]){ return false; } j++; } return true; } }