34.最初と最後の位置golangでソートされた配列の要素を探します

34.最初と最後の位置にソートされた配列の要素を探します

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

私に

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 ビュー320 000 +

おすすめ

転載: blog.csdn.net/csdn_kou/article/details/104112306
おすすめ