原创

217. 存在重复元素

温馨提示:
本文最后更新于 2022年12月03日,已超过 887 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

217. 存在重复元素

时间复杂度:O(n)
空间复杂度:O(n)

class Solution {
    public boolean containsDuplicate(int[] nums) {
        // - 初始化一个Map ,key 存 元素的值,value 存元素的索引
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                return true;
            }
            map.put(nums[i],i);
        }
        return false;
    }
}
正文到此结束