[LeetCode]Find the Duplicate Number 寻找数组重复值
Find the Duplicate Number 寻找数组重复值
二分搜索
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return None
left, right = 1, len(nums)
while left < right:
mid = (left + right) // 2
cnt = 0
for n in nums:
if n <= mid:
cnt += 1
if cnt <= mid:
left = mid + 1
else:
right = mid
return left
关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法

浙公网安备 33010602011771号