版權聲明:本文為博主原創文章,未經博主允許不得轉載。
難度:容易
要求:
實現一個算法確定字符串中的字符是否均唯一出現
樣例
給出"abc",返回 true
給出"aab",返回 false
思路:
用數組計數,用到了計數排序的思想
public class Solution {
/**
* @param str: a string
* @return: a boolean
*/
public boolean isUnique(String str) {
// write your code here
int[] tmp = new int[256];
for(int i = 0; i < str.length(); i++){
int index = str.charAt(i);
if(++tmp[index] > 1){
return false;
}
}
return true;
}
}