C++算法:搜索插入位置-----递归变种二分查找法

题目:

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。

示例 1:

输入: [1,3,5,6], 5 输出: 2

示例 2:

输入: [1,3,5,6], 2 输出: 1

示例 3:

输入: [1,3,5,6], 7 输出: 4

思路:

变种二分查找法,有三种情况:一。当begin>end时,表明nums为空,则直接ret=0;二。当(begin == end - 1) || (begin == end)时,为正常二分查找到最后,需判断begin或end是否==target,或判断插入位置;三。递归二分查找

代码:

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        //思路:变种二分查找法,有三种情况:一。当begin>end时,表明nums为空,则直接ret=0;二。当(begin == end - 1) || (begin == end)时,为正常二分查找到最后,需判断begin或end是否==target,或判断插入位置;三。递归二分查找
        int ret = 0;
        binary_search(nums, target, 0, nums.size()-1, ret);
        
        return ret;
    }
private: 
    void binary_search(vector<int>& nums, int target, int begin, int end, int& ret)
    {
        //终止条件
        if(begin > end)  //nums为kong
        {
            ret = 0;
            return ;
        }
        
        if((begin == end - 1) || (begin == end))
        {
            if(nums[begin] >= target)
                ret = begin;
            else if(nums[end] >= target)
                ret = end;
            else
                ret = end+1;
            return ;
        }
        
        int mid = (begin+end)/2;
        
        if(nums[mid] > target)
            binary_search(nums, target, begin, mid-1, ret);
        else if(nums[mid] < target)
            binary_search(nums, target, mid+1, end, ret);
        else
        {
            ret = mid;
            return ;
        }
    }
};

猜你喜欢

转载自blog.csdn.net/qq_31820761/article/details/90897767
今日推荐