当前位置:网站首页>leetcode 35. Search insert location

leetcode 35. Search insert location

2022-06-12 06:22:00 Well, let me see

35. Search insert location
 Please add a picture description
Thought analysis :
Dichotomous template question .

AC Code :

class Solution {
    
public:
    int searchInsert(vector<int>& nums, int target) {
    
        if(nums.size() == 1) {
    
            if(nums[0] < target) return 1;
            else 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 + 1;
        else
            return r;
    }
};
原网站

版权声明
本文为[Well, let me see]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010610049945.html