每日一题 2019 - 03 - 25
题目:
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm’s runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example 1:
| 1 | Input: nums = [5,7,7,8,8,10], target = 8 | 
Example 2:
| 1 | Input: nums = [5,7,7,8,8,10], target = 6 | 
解法:
这个题让我们找出在一个排序好的数组中的某一个元素的起始位置与结束位置且要求时间复杂度为o(logn),那么很直接的思路就是使用二分查找的方式:
- 需要讲一点的就是,如果我们找到一个元素且这个元素有很多个重复出现,那么可以从这个元素往前推找到起始位置,从这个元素往后推找到终止位置,最后把这两个位置放到 vector中
代码:
| 1 | class Solution { |