34. Find a sorted array elements in the first and last position golang

34. Find a sorted array elements in the first and last position

https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/

Me

func searchRange(nums []int, target int) []int {
	result := make([]int, 2)

    result[0] = -1
    result[1] = -1
	for j, key := range nums {
		if key == target && result[0] == -1 {
			result[0] = j
            result[1] = result[0]
		} else if key == target && result[0] != -1{
            result[1] = j
        }
	}

    return result
}
Published 269 original articles · won praise 223 · views 320 000 +

Guess you like

Origin blog.csdn.net/csdn_kou/article/details/104112306