Bisection method

前言

关于二分法的基本思路我们在高中时就已经学过,我这里直接上代码。

实现代码

    int searchRange(vector<int>& nums, int target) {
        int l=0;int r=nums.size()-1;int t=0;
        while(l<=r)
        {
            t=l+(r-l)/2;
            if(nums[t]==target)return t;else
            if(nums[t]>target)r=t-1;
            else    l=t+1;
        }
        return -1;
    };

猜你喜欢

转载自blog.csdn.net/qq_36389986/article/details/112258997
今日推荐