Python 45ms O(logn)


  • 0
    class Solution(object):
        def singleNonDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if not nums: return 0
            i, j = 0, len(nums)-1
            while i < j:
                mid = (i+j)/2
                if (mid-i) % 2 == 0:
                    if nums[mid] == nums[mid-1]: 
                        j = mid-2
                    else:
                        if nums[mid] != nums[mid+1]: return nums[mid]
                        i = mid+2
                else:
                    if nums[mid] == nums[mid-1]:
                        i = mid+1
                    else:
                        j = mid-1
            return nums[j]
    

Log in to reply
 

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