logo头像
Snippet 博客主题

【Leetcode 217】Contains Duplicate

难度: 中等(Medium)

题目

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
8
9
10
11
12
13
14
15
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if nums==None or len(nums)==0:
return False
unique_nums = set()
for n in nums:
if n in unique_nums:
return True
else:
unique_nums.add(n)
return False

评论系统未开启,无法评论!