my logn python solution


  • 0
    B
    class Solution(object):
        def singleNonDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            low = 0
            high = len(nums)-1
            while low<high:
                mid = (low+high)/2
                if mid-1 >= low and nums[mid]==nums[mid-1]:
                    if (mid-low)%2==1:
                        low = mid+1
                    else:
                        high = mid-2
                elif mid+1 <= high and nums[mid]==nums[mid+1]:
                    if (high-mid)%2==1:
                        high = mid-1
                    else:
                        low = mid+2
                elif nums[mid]!=nums[mid+1] and nums[mid]!=nums[mid-1]:
                    high=mid
                    low=mid
                elif nums[mid+1]==nums[mid]:
                    high = mid-1
                else:
                    low = mid+1
            return nums[low]

Log in to reply
 

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