码迷,mamicode.com
首页 > 其他好文 > 详细

Contains Duplicate

时间:2017-09-18 19:48:04      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:简单题   思路   integer   eve   fun   set   logs   find   obj   

    这道题为简单题

  题目:

    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、我是用的字典,如果已经在字典键中,就返回True,否则就把该值和引索值添加到字典中

    2、我看见有个大神代码更简单,直接比较现有的长度和set(该列表)的长度,如果一样就返回True,否则False

  代码:

    我的:

 1 class Solution(object):
 2     def containsDuplicate(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: bool
 6         """
 7         a = {}
 8         for i in nums:
 9             if i in a: return True
10             a[i] = i
11         return False

    大神:

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

 

Contains Duplicate

标签:简单题   思路   integer   eve   fun   set   logs   find   obj   

原文地址:http://www.cnblogs.com/liuxinzhi/p/7544607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!