题解 | #二分查找-II#(查找第一个元素大于目标值的下标)
二分查找-II
http://www.nowcoder.com/practice/4f470d1d3b734f8aaf2afb014185b395
class Solution { public: int search(vector<int>& nums, int target) { int low = 0, high = nums.size(); while(low < high){ int mid = low + (high - low)/2; // mid = low + high>>1 nums[mid] < target ? low = mid + 1 : high = mid; } return low<nums.size()&&nums[low] == target ? low: -1; // 查找的值可能越界需要判断。 } };