题解 | #在升序数组中查找元素的位置#
在升序数组中查找元素的位置
http://www.nowcoder.com/practice/f2628d9f7b7d4568aebaae6556aacfe0
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param target int整型 * @return int整型vector */ vector<int> searchRange(vector<int>& nums, int target) { // write code here vector<int> res(2, -1); int start = search(nums, target); if(nums[start] != target || nums.size() == start) return res; return {start, search(nums, target+1) - 1}; } int search(vector<int>& nums, int target){ int left = 0; int right = nums.size(); while(left < right){ int mid = left + (right - left) / 2; if(nums[mid] < target){ left = mid + 1; } else{ right = mid; } } return right; } };
https://www.cnblogs.com/grandyang/p/4409379.html