思路:只要是判断字符or数组中的元素是否重复or唯一,都是用哈希算法实现。 具体:使用HashMap,将key设为元素,value为元素出现次数,通过判断value值确定字符是否唯一

import java.util.*; public class Solution {

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param str string字符串 
 * @return bool布尔型
 */
public boolean isUnique (String str) {
    // write code here
     HashMap<Character,Integer> map = new HashMap<>();
    for(Character ch:str.toCharArray()){
        if(!map.keySet().contains(ch)){
            map.put(ch,1);
        }
        else {
            map.put(ch,map.get(ch)+1);
        }
    }
    for(Character ch:map.keySet()){
        if(map.get(ch)>1){
            return false;
        }
    }
    return true;
}

}