LintCode - 判斷字符串是否沒有重復字符(普通)

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

難度:容易
要求:

實現一個算法確定字符串中的字符是否均唯一出現
樣例
給出"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;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容