标签:tor vector -- 最小 int div 相等 sea bsp
class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { vector<int> r(2, -1); if (!nums.size()) { return r;//为空时,返回[-1,-1] } int l = 0, h = nums.size() - 1, m = 0; if (target < nums[l] || nums[h] < target) { return r; //小于最小或大于最大返回[-1,-1] } while (l <= h) { m = (l + h) / 2; if (nums[m] == target) { //命中m时 int i = m; while (nums[i] == target&&i < nums.size()) { i++;//寻找nums中与target相等值的右界 } r[1] = i - 1; while (nums[m] == target&&m >= 0) { m--;//寻找nums中与target相等值的左界 } r[0] = m + 1; return r; } else if (nums[m] < target) { l = m + 1; } else { h = m - 1; } } return r;//没找到target,返回[-1,-1] } };
标签:tor vector -- 最小 int div 相等 sea bsp
原文地址:https://www.cnblogs.com/asenyang/p/9747748.html