给定一个字符串string iniString,请返回一个bool值,True代表字符串的所有字符全都不同,False代表存在相同的字符。保证字符串中的字符为ASCII字符且不允许使用额外的存储结构,字符串的长度小于等于3000。大小写字母算不同的字符
测试样例:
"aeiou"
返回:True
"BarackObama"
返回:False
import java.util.*; public class Different { public boolean checkDifferent(String iniString) { int iniLength = iniString.length(); char[] charArray = iniString.toCharArray(); TreeSet<Character> after = new TreeSet<Character>(); for(int i = 0;i<charArray.length;i++){ after.add(charArray[i]); } int afterLength = after.size(); if(afterLength==iniLength) return true; return false; } }
class Different { public: bool checkDifferent(string iniString) { // write code here for(size_t i = 0; i < iniString.size(); i++) { for(size_t j = 0; j < iniString.size(); j++) { if((iniString[i] == iniString[j]) && (i != j)) return false; } } return true; } };
//这道题理解起来并不是很难 public static boolean checkDifferent(String iniString) { //先将字符串转换成字符数组 char[] str = iniString.toCharArray(); //定义跳出二层循环条件 boolean find = false; for (int i = 0; i < str.length && !find; i++) { for (int j = i+1; j < str.length; j++) { //当发现相同字符时,修改条件,直接跳出二层循环 if (str[i] == str[j]) { find = true; break; } } } //返回查找结果 return !find; }
class Different { public: bool checkDifferent(string iniString) { // write code here sort(iniString.begin(),iniString.end()); for(int i=0;i<iniString.size();i++) { if(iniString[i]==iniString[i-1]) 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(i) == iniString.charAt(j)) return false; } return true; // write code here } }
import java.util.*; public class Different { public boolean checkDifferent(String iniString) { // write code here if(iniString.length() > 256) return false; boolean[] char_set = new boolean[65535]; for(int i = 0; i < iniString.length(); i++){ int val = iniString.charAt(i); if(char_set[val]) return false; char_set[val] = true; } return true; } }
import java.util.*; public class Different { public boolean checkDifferent(String iniString) { // write code here HashSet<Character> hashset=new HashSet<>(); for(int i=0;i<iniString.length();i++){ if(hashset.contains(iniString.charAt(i))){ return false; }else{ hashset.add(iniString.charAt(i)); } } return true; } }
class Different { public: bool checkDifferent(string iniString) { if (iniString.length() > 256) return false; int length = 256; int *result = (int *)malloc(length*sizeof(int)); memset(result, 0, length*sizeof(int)); for (int i = 0; i < iniString.length(); i++) { int index = iniString[i]; if(result[index] == 1) return false; result[index]++; }// write code here return true; } };下面不行:
class Different { public: bool checkDifferent(string iniString) { if (iniString.length() > 256) return false; int result[256] = { 0 }; for (int i = 0; i < iniString.length(); i++) { int index = iniString[i]; result[index]++; if (result[index] > 1) return false; }// write code here return true; } };报错为:
class Different { public: bool checkDifferent(string iniString) { sort(iniString.begin(),iniString.end()); int num=0,i=0; num=iniString.size(); for ( i = 1; i < num; i++) { if (iniString[i] == iniString[i-1]) { return false; } } return true; } };我的做法是先进行排序然后在查找是否前后有相同的字符
import java.util.*; public class Different { public boolean checkDifferent(String iniString) { // write code here List<Character> charList = new ArrayList<Character>(); for (int i = 0; i < iniString.length(); i++) { char c = iniString.charAt(i); if (charList.contains(c)) { return false; } charList.add(c); } return true; } }
个人认为是面向对象编程而言最好的答案
public class Different { public boolean checkDifferent(String iniString) { boolean[] arr = new boolean[65536]; // 不用额外空间就是空间复杂度O(1) int len = iniString.length(); for (int i = 0; i < len; ++i) { char c = iniString.charAt(i); if (arr[c]) return false; arr[c] = true; } return true; } }