leetcode-----35. 搜索插入位置

代码

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        if (nums.size() == 0) return 0;

        int l = 0, r = nums.size() - 1;

        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] == target) return mid;
            else if (nums[mid] > target) r = mid - 1;
            else l = mid + 1;
        }
        return l;
    }
};
posted @ 2020-06-26 15:44  景云ⁿ  阅读(55)  评论(0编辑  收藏  举报