【Leetcode_总结】961. 重复 N 次的元素 - python

Q:

在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次。

返回重复了 N 次的那个元素。

示例 1:

输入:[1,2,3,3]
输出:3

示例 2:

输入:[2,1,2,5,3,2]
输出:2

示例 3:

输入:[5,1,5,2,5,3,5,4]
输出:5

链接:https://leetcode-cn.com/problems/n-repeated-element-in-size-2n-array/

思路:这个题目很简单 词频大于1就好了

代码:

class Solution:
    def repeatedNTimes(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        
        dic = {}
        for a in A:
            if a in dic:
                return a
            else:
                dic[a] = 1

猜你喜欢

转载自blog.csdn.net/maka_uir/article/details/85269411
今日推荐