Python simple binary search runs in 39ms


  • 0
    J

    It's a classic 3 way recursive solution.

    class Solution(object):
        def searchInsert(self, nums, target):
            return self.BinarySearch(0,len(nums),nums,target)
    
        def BinarySearch(self,start, end, list, target):
    		
        	index = (start+end)/2
        	
        	if start==end: return index
        
        	if target < list[index]:
        		return self.BinarySearch(start,index,list,target)
        	elif target > list[index]:
        		return self.BinarySearch(index+1,end,list,target)
        	else:		
        		return index
    

Log in to reply
 

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