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

被折叠的 条评论
为什么被折叠?



