题解 | #求目标值的区间#
求目标值的区间
https://www.nowcoder.com/practice/e302d06bcdac4171b9768fd354922880
class Solution { public: /** * * @param A int整型一维数组 * @param n int A数组长度 * @param target int整型 * @return int整型vector */ vector<int> searchRange(int* A, int n, int target) { int l = 0, h = n, m; int start = -1, end = -1; while (l < h) { m = (l + h) / 2; if (A[m] > target) { h = m; } else if (A[m] < target) { l = m + 1; } else { int index = m; while (index >= 0 && A[index] == target)index--; start = index + 1; index = m; while (index < n && A[index] == target)index++; end = index - 1; break; } } vector<int> res; res.push_back(start); res.push_back(end); return res; } };