LeetCode 35. 搜索插入位置

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

        if(nums[r] >= target) return r;
        else return r + 1; 

    }
};

你可能感兴趣的:(LeetCode)