35. 搜索插入位置

35. 搜索插入位置


题目链接:35. 搜索插入位置

代码如下:

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int low=0,high=nums.size()-1;
        int mid=-1;    

        while(low<=high)
        {
            mid=low+(high-low)/2;
            if(nums[mid]==target)
                return mid;
            else if(nums[mid]<target)
                low=mid+1;
            else 
                high=mid-1;
        }

        return high+1;//参考代码随想录
    }
};

你可能感兴趣的:(leetcode,c++)