存在重复元素 (Contains Duplicate)

 

思路:

// @Title: 存在重复元素 (Contains Duplicate)
// @Author: qisiii
// @Date: 2021-08-16 13:58:33
// @Runtime: 7 ms
// @Memory: 44.3 MB
// @comment: 
// @flag: 
class Solution {
    public boolean containsDuplicate(int[] nums) {
        HashSet hashSet=new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (!hashSet.contains(nums[i])){
                hashSet.add(nums[i]);
            }else {
                return true;
            }
        }
        return false;
    }
}

+++ title = “存在重复元素 (Contains Duplicate)” draft = false +++

思路:

// @Title: 存在重复元素 (Contains Duplicate)
// @Author: qisiii
// @Date: 2022-02-28 22:38:35
// @Runtime: 10 ms
// @Memory: 53.6 MB
// @comment: 
// @flag: 
class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set shuzu=new HashSet();
        for(int i:nums){
            if(shuzu.contains(i)){
                return true;
            }else{
                shuzu.add(i);
            }
        }
        return false;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18