Leetcode 35. 搜索插入位置

(l,r]找low

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
    vector<int> ans;
    int l = -1, r = (int)nums.size()-1;
    while (l < r) {
        int mid = (l + r+1) >> 1;
        if (nums[mid] < target) l = mid;
        else r = mid - 1;
    }
    return ++l;
}
};

猜你喜欢

转载自blog.csdn.net/Bendaai/article/details/80075815