leetcode二分查找算法题

目录

  • 1.二分查找
  • 2.在排序数组中查找元素的第一个和最后一个位置
  • 3.x的平方根
  • 4.搜索插入位置
  • 5.山脉数组的峰顶索引
  • 6. 寻找峰值
  • 7.寻找旋转排序数组中的最小值
  • 8.8.0~n-1中缺失的数字

1.二分查找

二分查找
leetcode二分查找算法题_第1张图片

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)/2;
            if(nums[mid]<target) left = mid+1;
            else if(nums[mid]>target) right = mid-1;
            else return mid;
        }
        return -1;
    }
};

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

在排序数组中查找元素的第一个和最后一个位置
leetcode二分查找算法题_第2张图片

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        if(nums.size() == 0) return {-1,-1};
        int begin =-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;
        }
        return {begin,right};
    }
};

3.x的平方根

x的平方根
leetcode二分查找算法题_第3张图片

class Solution {
public:
    int mySqrt(int x) {
        if(x<1) 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;
    }
};

4.搜索插入位置

搜索插入位置
leetcode二分查找算法题_第4张图片

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;
    }
};

5.山脉数组的峰顶索引

山脉数组的峰顶索引
leetcode二分查找算法题_第5张图片

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;
    }
};

6. 寻找峰值

寻找峰值
leetcode二分查找算法题_第6张图片

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]) right=mid;
            else left = mid+1;
        }
        return left;
    }
};

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

寻找旋转排序数组中的最小值
leetcode二分查找算法题_第7张图片

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

8.8.0~n-1中缺失的数字

0~n-1中缺失的数字
leetcode二分查找算法题_第8张图片

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;
        }
        return records[left]==left?left+1:records[left]-1;
    }
};

你可能感兴趣的:(算法,算法,leetcode,职场和发展)