Binary search with python and beats 98.2%


  • 0
    E
    class Solution(object):
        def singleNonDuplicate(self, nums):
            l=len(nums)
            st,en=0,l-1
            while st<=en:
                mid=(st+en)/2
                if mid>0 and nums[mid]==nums[mid-1]:
                    if mid%2==1:st=mid+1
                    else:en=mid-2
                elif mid<l-1 and nums[mid]==nums[mid+1]:
                    if mid%2==0:st=mid+2
                    else:en=mid-1
                else:return nums[mid]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.