classSolution(object): deffindDuplicate(self, nums): """ :type nums: List[int] :rtype: int """ start = 1 end = len(nums) - 1 while start <= end: mid = start + int((end - start) / 2) c = self.count(nums=nums, start=start, end=mid) if start == end: if c > 1: return start else: break if c > (mid - start + 1): end = mid else: start = mid + 1 return-1 defcount(self, nums, start, end): n = 0 for i in range(len(nums)): if nums[i] >= start and nums[i] <= end: n += 1 return n
if __name__ == '__main__': s = Solution() t = [1,3,4,2,2] print(s.findDuplicate(nums=t))