TreeviewCopyright © aleen42 all right reserved, powered by aleen42

217. 存在重复元素

https://leetcode-cn.com/problems/contains-duplicate/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-21 20:36:30
 * @LastEditTime: 2020-08-21 20:36:47
 * @Description: https://leetcode-cn.com/problems/contains-duplicate/
 * @FilePath: \leetcode-googtech\#217. Contains Duplicate\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {

    // 利用 HashSet 集合判断数组中是否还有重复元素
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> result = new HashSet<>();
        for(int num : nums) result.add(num);
        return result.size() == nums.length ? false : true;
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-21 20:36:36
LastEditTime: 2020-08-21 20:37:02
Description: https://leetcode-cn.com/problems/contains-duplicate/
FilePath: \leetcode-googtech\#217. Contains Duplicate\Solution.py
WebSite: https://algorithm.show/
'''

class Solution(object):

    # 利用 HashSet 集合判断数组中是否还有重复元素
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        return len(set(nums)) < len(nums)
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""