思路
本题 和 前置知识2 中的 实战:【二分2】二分找下标问题 没有本质区别,先搞懂那个题即可。 #code-switcher
class Solution:
def find_first_position(self, nums: List[int], target: int) -> int:
n = len(nums)
left, right = 0, n - 1
Leetcode 34.在排序数组中查找元素的第一个和最后一个位置-原题链接
给定一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
你必须设计并实现时间复杂度为 O(logn) 的算法解决此问题。
nums。target,表示要查找的目标值。target 在数组中的起始位置和结束位置。如果 target 不在数组中,则输出 -1 -1。6
5 7 7 8 8 10
8
3 4
6
5 7 7 8 8 10
6
-1 -1
nums 是一个非递减数组
By signing up a CodeFun2000 universal account, you can submit code and join discussions in all online judging services provided by us.