A golang solution


  • 0
    N
    func searchRange(nums []int, target int) []int {
        result := []int{-1, -1}
        
        if len(nums) <= 0 || nums[len(nums) - 1] < target || nums[0] > target {
            return result
        }
        
        low := 0;
        high := len(nums) - 1
        
        for low <= high {
            mid := (low + high) / 2;
            if nums[mid] == target {
                if result[1] == -1 || mid > result[1] {
                    result[1] = mid
                }
                if result[0] == -1 || mid < result[0] {
                    result[0] = mid
                }
                if v := searchRange(nums[0:mid], target); v[0] != -1 && v[0] < mid {
    		result[0] = v[0]
    	    }
    	    if v := searchRange(nums[mid + 1:len(nums)], target); v[1] != -1 {
    		result[1] = v[1] + mid + 1
    	    }
                break
            } else if nums[mid] < target {
                low = mid + 1
            } else {
                high = mid - 1
            }
        }
        return result
    }
    

Log in to reply
 

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