287. 寻找重复数

*爱你&永不变心* 提交于 2020-03-10 10:25:24
class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        # method 1
        # flags = [True]*(len(nums))
        # for val in nums:
        #     if flags[val-1]:
        #         flags[val-1] = False
        #     else:
        #         return val

        # method 2
        # temp = sorted(nums)
        # for i in range(1, len(temp)):
        #     if temp[i] == temp[i-1]:
        #         return temp[i]

        # method 3
        # counts = [0]*len(nums)
        # for val in nums:
        #     counts[val-1] += 1
        #     if counts[val-1]>1:
        #         return val
        
        # # method 4
        # def count_le(left, mid):
        #     res = 0
        #     for val in nums:
        #         if left<= val <= mid:
        #             res += 1
        #     return res

        # left, right = 1, len(nums)
        # while left < right:
        #     mid = left+(right-left)//2
        #     if count_le(left, mid) <= mid-left+1:
        #         left = mid + 1
        #     else:
        #         right = mid
        # return left
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!