题意:找到非严格递增的数组中和target相等的左右边界
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
解答: 经典二分,找左右边界,要查看l是否满足题意
class Solution {
public:vector<int> searchRange(vector<int>& a, int t) {if (a.empty()) return {-1, -1};int first = findFirst(a, t);int second = findLast(a , t);return {first, second};}int findFirst(vector<int>& a, int t) {int l = 0;int r = a.size() - 1;while (l < r) {int mid = l + (r - l) / 2;if(a[mid] >= t) {r = mid;} else {l = mid + 1;}}return a[l] == t ? l : -1;}int findLast(vector<int>& a, int t) {int l = 0;int r = a.size() - 1;while (l < r) {int mid = l + (r - l) + 1 / 2;if(a[mid] <= t) {l = mid;} else {r = mid - 1;}}return a[l] == t ? l : -1;}};
算法复杂度:O(2logN), N是数组长度,空间复杂度O(1)