leetcode_search_for_a_range

难度:Medium

解题思路:一个low,一个high,分别从数组的首尾往中心走,查找target。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int low = 0;
int high = nums.size()-1;
while(low <= high && low <= nums.size()-1 && high >= 0)
{
if(nums[low] == target && nums[high] == target)
return {low,high};
if(nums[low] != target)
low++;
if(nums[high]!=target)
high--;
}
return {-1,-1};
}
};

运行结果:9ms,超过40.86%