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;
}
};

你可能感兴趣的:(Leetcode 35. 搜索插入位置)