Skip to content

Latest commit

 

History

History
58 lines (39 loc) · 1.17 KB

数组中重复的数字.md

File metadata and controls

58 lines (39 loc) · 1.17 KB

剑指 Offer 03. 数组中重复的数字

https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/

题目描述

找出数组中重复的数字。


在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。

示例 1:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

判断当前数字是不是已经在数组中了

python代码

执行用时: 48 ms , 在所有 Python3 提交中击败了 81.63% 的用户 内存消耗: 23.5 MB , 在所有 Python3 提交中击败了 10.73% 的用户

class Solution:
    def findRepeatNumber(self, nums: List[int]) -> int:
        vals=set()
        for item in nums:
            if(item not in vals):
                vals.add(item)
            else:
                return item