【C++算法】二分查找

 二分查找

  • 题目链接

二分查找icon-default.png?t=O83Ahttps://leetcode.cn/problems/binary-search/

  • 算法原理

  • 代码步骤

  • 代码展示
class Solution {
public:
    int search(vector<int>& nums, int target) 
    {
        int left = 0, right = nums.size() - 1;
        while(left <= right)
        {
            // 防止溢出
            int mid = left + (right - left + 1) / 2;
            if(nums[mid] < target) 
            {
                left = mid + 1;
            }
            else if(nums[mid] > target)
            {
                right = mid - 1;
            }
            else 
            {
                return mid;
            }
        }
        return -1;
    }
};

在排序数组中查找元素的第一个和最后一个位置

  • 题目链接

在排序数组中查找元素的第一个和最后一个位置icon-default.png?t=O83Ahttps://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/

  • 算法原理

  • 代码步骤

  • 代码展示
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) 
    {
        if(nums.size() == 0)
        {
            return {-1, -1};
        }
        // 设置左端点和右端点
        int begin = -1, end = -1;

        int left = 0, right = nums.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target)
            {
                left = mid + 1;
            }
            else 
            {
                right = mid;
            }
        }
        if(nums[left] != target) return {-1, -1};
        begin = left;

        left = 0, right = nums.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if(nums[mid] <= target)
            {
                left = mid;
            }
            else 
            {
                right = mid - 1;
            }
        }
        if(nums[right] != target) return {-1, -1};
        end = right;
        return {begin, end};
    }
};

x的平方根

  • 题目链接

x的平方根icon-default.png?t=O83Ahttps://leetcode.cn/problems/sqrtx/

  • 算法原理

  • 代码展示
class Solution {
public:
    int mySqrt(int x) 
    {
        if(x == 0) return 0;
        int left = 1, right = x;
        while(left < right)
        {
            long long mid = left + (right - left + 1) / 2;
            if(mid * mid <= x)
            {
                left = mid;
            }
            else 
            {
                right = mid - 1;
            }
        }
        return left;
    }
};

搜索插入位置

  • 题目链接

搜索插入位置icon-default.png?t=O83Ahttps://leetcode.cn/problems/search-insert-position/description/

  • 算法原理

  • 代码展示
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) 
    {
        int left = 0, right = nums.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        if(nums[left] < target) return left + 1;
        return left;
    }
};

山脉数组的峰顶索引

  • 题目链接

山脉数组的峰顶索引icon-default.png?t=O83Ahttps://leetcode.cn/problems/peak-index-in-a-mountain-array/description/

  • 算法原理

  • 代码展示
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) 
    {
        // 最左侧和最右侧元素不可能使山顶
        int left = 1, right = arr.size() - 2;
        while(left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if(arr[mid] > arr[mid - 1])
            {
                left = mid;
            }
            else
            {
                right = mid - 1;
            }
        }
        return left;
    }
};

寻找峰值

  • 题目链接

寻找峰值icon-default.png?t=O83Ahttps://leetcode.cn/problems/find-peak-element/

  • 算法原理

  • 代码展示
class Solution {
public:
    int findPeakElement(vector<int>& nums) 
    {
        int left = 0, right = nums.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] < nums[mid+1])
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        return left;
    }
};

寻找旋转排序数组中的最小值

  • 题目链接

寻找旋转排序数组中的最小值icon-default.png?t=O83Ahttps://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/description/

  • 算法原理

  • 代码展示
class Solution {
public:
    int findMin(vector<int>& nums) 
    {
        int left = 0, right = nums.size() - 1;
        int n = nums.size();
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] > nums[n - 1])
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        return nums[left];
    }
};

点名

  • 题目链接

点名icon-default.png?t=O83Ahttps://leetcode.cn/problems/que-shi-de-shu-zi-lcof/description/

  • 算法原理

  • 代码展示
class Solution {
public:
    int takeAttendance(vector<int>& records) 
    {
        int left = 0, right = records.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(records[mid] == mid)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        // if(left == records.size() - 1 && records[left] == left) return left + 1;
        // else return left;
        return left == records[left] ? left + 1 : left;
    }
};
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值