Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Last active October 23, 2017 17:51
Show Gist options
  • Save cixuuz/af2668d9b03e83f93d056323d0636ecd to your computer and use it in GitHub Desktop.
Save cixuuz/af2668d9b03e83f93d056323d0636ecd to your computer and use it in GitHub Desktop.
[287. Find the Duplicate Number] #leetcode
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) > 1:
slow = nums[0]
fast = nums[nums[0]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]]
fast = 0
while fast != slow:
fast = nums[fast]
slow = nums[slow]
return slow
return -1
class Solution(object):
# o(nlgn)
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
low = 1
high = len(nums) - 1
while low < high:
mid = low + (high-low) // 2
# count
count = 0
for i in nums:
if i <= mid:
count += 1
if count <= mid:
low = mid + 1
else:
high = mid
return low
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment