LeetCode 217. Contains Duplicate

题目

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

分析

判断是否包含重复数

代码

1
2
3
4
5
6
7
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(nums) != len(set(nums))

点评

直接用 set 去重,或者先用字典记录一次出现的次数,然后直接遍历一次,也可以