题目要求
In a array A
of size 2N
, there are N+1
unique elements, and exactly one of these elements is repeated N times.
Return the element repeated N
times.
题目分析及思路
题目给出一个长度为2N的数组,其中有N+1个不同元素,有一个元素重复了N次,要求返回该重复N次的元素。可以随机从数组中获得两个元素,如果这两个元素相同,则该元素就是题目所要返回的元素。
python代码
class Solution:
def repeatedNTimes(self, A):
"""
:type A: List[int]
:rtype: int
"""
while 1:
num = random.sample(A,2)
if num[0]==num[1]:
return num[0]