What's the problem with my Python code (binary search for Missing Number)?


  • -1
    L

    I can get the right output when I run this code, but the leetcode didn't accept it. What's the possible problem with it?

    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            first=0
            last=len(nums)-1
            while first<=last:
                mid=(first+last)//2
                if mid<nums[mid] and (mid-1)==nums[mid-1]:
                    break
                elif mid<nums[mid] and (mid-1)<nums[mid-1]:
                    last=mid-1
                elif mid==nums[mid]:
                    first=mid+1
            return mid

  • 0
    B

    The list of number has not to be sorted, so your binary search algo. won't work.


Log in to reply
 

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