腾讯五十题 No.39 存在重复元素

题目链接
Set实现

Map实现

class Solution {
    public boolean containsDuplicate(int[] nums) {
        int len = nums.length;
        Map<Integer,Integer> map = new HashMap<>(len);
        for(int i = 0;i<len;i++){
            if(map.containsKey(nums[i])){
                return true;
            }
            map.put(nums[i],i);
        }
        return false;
    }
}
posted @ 2022-02-09 03:40  蹇爱黄  阅读(23)  评论(0)    收藏  举报