34. 在排序数组中查找元素的第一个和最后一个位置 golang

34. 在排序数组中查找元素的第一个和最后一个位置

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
}
发布了269 篇原创文章 · 获赞 223 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/csdn_kou/article/details/104112306