Gopherville golang solution


  • 0
    E
    import (
        "math"
    )
    func searchInsert(nums []int, target int) int {
        start := 0
        end := len(nums) - 1
        pos := binSearch(&nums, start, end, target)
        return pos
    }
    
    func binSearch(arr *[]int, start,end, target int) int {
    
        nums := *arr
    
        if (start > end) {
            return start
        }
        if target == nums[(end+start)/2] {
            return (end+start)/2
        }
        if target > nums[(end+start)/2] {
            start = (end+start)/2
            return binSearch(arr, start+1, end, target)
            
        }
        if target < nums[(end+start)/2] {
            end = (end+start)/2
            return binSearch(arr, start, end-1, target)
        } 
        return -1
    }

Log in to reply
 

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