剑指offer刷题笔记||03.数组中重复的数字(python)

剑指offer刷题笔记||03.数组中重复的数字(python)

题目描述:
剑指offer刷题笔记||03.数组中重复的数字(python)_第1张图片

解题思路

因为题目只要求输出其中的一个重复数,所以可以对其经进行排序,然后用第一个数对后面进行比较,找到重复值便返回即可。

我的代码:

class Solution(object):
    def findRepeatNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        pre = nums[0]
        for i in nums[1:]:
            if pre == i:
                return pre
            pre = i

提交结果:
剑指offer刷题笔记||03.数组中重复的数字(python)_第2张图片

你可能感兴趣的:(剑指offer,leetcode,python,leetcode,python,算法)