首页 > 其他分享 >LeetCode二分搜索

LeetCode二分搜索

时间:2022-10-07 11:44:57浏览次数:85  
标签:二分 right target nums int mid 搜索 LeetCode left

Search in Rotated Sorted Array

LeetCode/力扣

先判断中间的和尾部的数字大小,再判断target和首尾中三个数字大小关系,如此便能进行二分搜索

int search(vector<int>& nums, int target) {
    if (nums.empty()) return -1;
    int left = 0, right = nums.size() - 1;
    while(left < right) {
        if(nums[mid] > nums[right]) {
            if(target > nums[mid] || target < nums[left]){
                left = mid + 1;
            }else {
                right = mid;
            }
        } else {
            if (target > nums[mid] && target <= nums[right]){
                left = mid + 1;
            } else {
                right = mid;
            }
        }
    }
    return nums[left] == target ? left : -1;
}

Search a 2D Matrix

LeetCode/力扣

先找到所在的行,再找所在的列

bool searchMatrix(vector<vector<int>>& matrix, int target) {
    int m = matrix.size();
    if (m == 0) return false;
    int n = matrix[0].size();
    if (n == 0) return false;
    
    int row = 0;
    int left = 0, right = m;
    
    while(left < right) {
        int mid = left + (right - left) / 2;
        
        if (matrix[mid][0] <= target && matrix[mid][n - 1] >= target) {
            row = mid;
            break;
        } else if (matrix[mid][0] > target) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    
    left = 0; right = n;
    while(left < right) {
        int mid = left + (right - left) / 2;
        
        if(matrix[row][mid] == target){
            return true;
        }else if(matrix[row][mid] < target) {
            left = mid + 1;
        }else {
            right = mid;
        }
    }
    return false;
}

Search in Rotated Sorted Array II

LeetCode/力扣

bool search(vector<int>& nums, int target) {
    int n = nums.size();
    int l = 0;
    int r = n;
    int m;
    while(l!=r){
        m = l+(r-l)/2;
        if(nums[m] == target)
            return true;
        if(nums[l] == nums[m]){
            l++;continue;
        }
        if(nums[l]<nums[m]){
            if(nums[l]<=target && target<nums[m])
                r = m;
            else
                l = m+1;
        }
        else{
            if(nums[m]<target && target<=nums[r-1])
                l = m+1;
            else
                r = m;
        }
    }
    return false;
}

Find Minimum in Rotated Sorted Array

LeetCode/力扣

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

Find Peak Element

LeetCode/力扣

int findPeakElement(vector<int>& nums) {
    return search(nums, 0, nums.size() -1);
}
int search(vector<int>& nums, int l, int r) {
    if (l == r) return l;
    int mid = l + (r - l) / 2;
    if(nums[mid] > nums[mid + 1])
        return search(nums, l, mid);
    
    return search(nums, mid+1, r);
}

Count of Range Sum

LeetCode/力扣

int countRangeSum(vector<int>& nums, int lower, int upper) {
    int res = 0;
    long long sum = 0;
    multiset<long long> sums;
    sums.insert(0);
    for (int i = 0; i < nums.size(); ++i) {
        sum += nums[i];
        res += distance(sums.lower_bound(sum - upper), sums.upper_bound(sum - lower));
        sums.insert(sum);
    }
    return res;
}

Find Smallest Letter Greater Than Target

LeetCode/力扣

char nextGreatestLetter(vector<char>& letters, char target) {
    int left = 0;
    int right = letters.size() - 1;
    while(left <= right) {
        int mid = left + (right - left) / 2;
        if(letters[mid] == target){
            left = mid + 1;
            // break;
        }else if (letters[mid] < target) {
            left = mid + 1;
        }else {
            right = mid - 1;
        }
    }
    
    return left >= letters.size() ? letters[0] : letters[left];
}

LeetCode/力扣

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

Peak Index in a Mountain Array

LeetCode/力扣

int peakIndexInMountainArray(vector<int>& A) {
    int left = 0;
    int right = A.size() - 1;
    while(left <= right) {
        int mid = left + (right - left) / 2;
        if (A[mid] > A[mid + 1]) {
            right = mid - 1;
        }else {
            left = mid + 1;
        }
    }
    return left;
}

标签:二分,right,target,nums,int,mid,搜索,LeetCode,left
From: https://www.cnblogs.com/xnzone/p/16759364.html

相关文章

  • 二分图
    二分图的判定不存在奇数环。可以通过匈牙利算法,染色判定。booldfs(intx,intt){ st[x]=t; for(inti=head[x];i;i=ne[i]) { inty=ver[i]; if(!st[y]) { ......
  • LeetCode20 有效的括号
    给定一个只包括'(',')','{','}','[',']' 的字符串s,判断字符串是否有效。有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。每个右括号都......
  • LeetCode138 复制带随机指针的链表
     idea: 刚开始没有思路,被题目弄懵了,我能想到的方法就是先复制不带random指针的链表,之后由表头到表尾再将每个结点的random指针通过循环进行连接,时间复杂度肯定时很高......
  • 【数据结构和算法】LeetCode,初级算法-16验证回文串
    截止到目前我已经写了600多道算法题,其中部分已经整理成了pdf文档,目前总共有1000多页(并且还会不断的增加),大家可以免费下载下载链接:​​https://pan.baidu.com/s/1hjwK0ZeRxY......
  • LeetCode打卡
    目录927.三等分927.三等分https://leetcode.cn/problems/three-equal-parts/classSolution{public:vector<int>threeEqualParts(vector<int>&arr){......
  • 【数据结构和算法】LeetCode,初级算法-15有效的字母异位词
    截止到目前我已经写了600多道算法题,其中部分已经整理成了pdf文档,目前总共有1000多页(并且还会不断的增加),大家可以免费下载下载链接:​​https://pan.baidu.com/s/1hjwK0ZeRxY......
  • 【数据结构和算法】LeetCode,初级算法-14字符串中的第一个唯一字符
    截止到目前我已经写了600多道算法题,其中部分已经整理成了pdf文档,目前总共有1000多页(并且还会不断的增加),大家可以免费下载下载链接:​​https://pan.baidu.com/s/1hjwK0ZeRxY......
  • LeetCode(剑指 Offer)- 61. 扑克牌中的顺子
    题目链接:​​点击打开链接​​题目大意:略解题思路相关企业字节跳动AC代码Java//解决方案(1)classSolution{publicbooleanisStraight(int[]nums){Set<In......
  • Java二分查找代码实现
    Java二分查找代码实现及原理简要分析代码原理描述前提:已经有一个排好序的数组(否则需要先排序)定义左边界left,右边界right,确定搜索范围,循环执行二分查找(第3、4步骤)......
  • CF1415D XOR-gun 题解 二分答案/贪心
    题目链接https://codeforces.com/problemset/problem/1415/D题目大意给定一个长为\(n\)的不降序列,每次操作可以任选相邻的两个数,并将这两个数替换为两个数按位异或的......