标签:targe tor 查找 大于等于 search https out 排序数组 while
题目链接:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/
时间复杂度:O(logn)
空间复杂度:O(1)
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size() - 1;
int l = 0;
int r = n + 1;
while(l < r)
{
int mid = l + r >> 1;
//找到第一个大于等于target的位置
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}
//cout << r << endl;
int res = 0;
int i = l;
while(i <= n && nums[i++] == target)
res++;
return res;
}
};
标签:targe tor 查找 大于等于 search https out 排序数组 while
原文地址:https://www.cnblogs.com/Trevo/p/13050314.html