实现一个算法确定字符串中的字符是否均唯一出现
给出"abc"
,返回 true
给出"aab"
,返回 false
public class Solution {
public boolean isUnique(String str) {
Set<Character> set = new HashSet<Character>();
for(int i = 0; i < str.length(); i++) {
if(set.contains(str.charAt(i)))
return false;
//没有则存入
set.add(str.charAt(i));
}
return true;
}
}
/*public class Solution {
public boolean isUnique(String str) {
// write your code here
boolean[] set = new boolean[256];
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
//为true返回false
if (set[val]) {
return false;
}
//否则设置为true;
set[val] = true;
}
return true;
}
}*/