https://leetcode.com/problems/find-all-duplicates-in-an-array/#/description
Python (超时了)
class Solution(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
d = dict()
for num in nums:
if num not in d.keys():
d[num] = 1
else:
d[num] += 1
return [k for k, v in d.items() if v == 2]
Python
- 不要用key in dict.keys() 很慢
- 用dict.has_key(key)很快
class Solution(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
d = dict()
re = []
for num in nums:
if not d.has_key(num):
d[num] = 1
else:
d[num] += 1
re.append(num)
return re