当前位置:网站首页>[leetcode] day91- duplicate elements exist

[leetcode] day91- duplicate elements exist

2022-07-01 06:20:00 Upside down, it's a circle

subject

217. There are duplicate elements 【 Simple 】

Answer key

class Solution {
    
    public boolean containsDuplicate(int[] nums) {
    
        Set<Integer>hashSet=new HashSet<>();
        for(int i=0;i<nums.length;i++){
    
            if(hashSet.contains(nums[i]))
                return true;
            hashSet.add(nums[i]);
        }
        return false;
    }
}

Time complexity : O ( n ) O(n) O(n)

Spatial complexity : O ( n ) O(n) O(n)

原网站

版权声明
本文为[Upside down, it's a circle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010608531272.html