首页 > 其他分享 >LeetCode 81. 搜索旋转排序数组 II

LeetCode 81. 搜索旋转排序数组 II

时间:2023-04-05 14:55:51浏览次数:38  
标签:index target nums int mid II start 81 LeetCode

1

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        int index = -1;
        for (int i = 0; i < nums.size() - 1; ++i){
            if (nums[i] > nums[i + 1]) index = i;
        }
        if (index == -1){
            index = nums.size() - 1;
        }
        return binarySearch(nums,target,-1,index) || binarySearch(nums,target,index,nums.size() - 1);

    }
    bool binarySearch(vector<int>& nums,int target,int l,int r){
        //左开右闭
        while (l < r){
            int mid = l + ((r - l) >> 1) + 1;
            if (nums[mid] < target){
                l = mid;
            }else if (nums[mid] > target){
                r = mid - 1;
            }else{
                return true;
            }
        }
        return false;
    }
};

2

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        //左闭右闭
        int start = 0;
        int end = nums.size()-1;
        while (start <= end){
            int mid = start + ((end - start) >> 1);
            if (nums[mid] == target) return true;
            if (nums[mid] == nums[start]) start++;
            else if (nums[mid] >= nums[start]){  
                //左区间增序
                if (nums[mid] > target && target >= nums[start]){
                    end = mid - 1;
                }else{
                    start = mid + 1;
                }
            }else{
                //右区间增序
                if (nums[end] >= target && target > nums[mid]){
                    start = mid + 1;
                }else{
                    end = mid - 1;
                }
            }
        }
        return false;
    }
};

标签:index,target,nums,int,mid,II,start,81,LeetCode
From: https://www.cnblogs.com/poteitoutou/p/17289431.html

相关文章

  • COMP3411/9814 人工智能
    COMP3411/9814ArtificialIntelligenceTerm1,2023Assignment3–PlanningandMachineLearningDue:Week10-10pmFriday21AprilMarks:10%offinalassessmentforCOMP3411/9814ArtificialIntelligenceQuestion1:Planning(4marks)Modifythesimpleplanner......
  • [LeetCode] 1339. Maximum Product of Splitted Binary Tree 分裂二叉树的最大乘积
    Giventhe root ofabinarytree,splitthebinarytreeintotwosubtreesbyremovingoneedgesuchthattheproductofthesumsofthesubtreesismaximized.Return themaximumproductofthesumsofthetwosubtrees.Sincetheanswermaybetoolarge,re......
  • [leetcode每日一题]4.5
    2427. 公因子的数目提示简单20相关企业给你两个正整数 a 和 b ,返回 a 和 b 的 公 因子的数目。如果 x 可以同时整除 a 和 b ,则认为 x 是 a 和 b 的一个 公因子 。 示例1:输入:a=12,b=6输出:4解释:12和6的公因子是1、2、3、6。示例2:输入:a=25,......
  • [LeetCode] 2405. Optimal Partition of String
    Givenastring s,partitionthestringintooneormore substrings suchthatthecharactersineachsubstringare unique.Thatis,noletterappearsinasinglesubstringmorethan once.Return the minimum numberofsubstringsinsuchapartition.Not......
  • 39. 组合总和 40.组合总和II 131.分割回文串
    39.组合总和自己写的回溯算法:classSolution{List<List<Integer>>list;LinkedList<Integer>res;publicList<List<Integer>>combinationSum(int[]candidates,inttarget){list=newArrayList<>();res=......
  • 216.组合总和III 17.电话号码的字母组合
    216.组合总和III回溯的常规思路做这道题:classSolution{List<List<Integer>>list=newArrayList<>();LinkedList<Integer>res=newLinkedList<>();publicList<List<Integer>>combinationSum3(intk,intn){f......
  • INS-41881处理
    GI升级时,跑完dryRunForUpgrade后再执行gridSetup.sh时候出现下面异常--/u01/app/19.0.0/19grid/gridSetup.sh-dryRunForUpgrade[INS-41881]InstallerhasdetectedthatthegroupspecifiedforOSDBAisnotsameasthegroup'asmdba'retrievedfromthecurrentconfig......
  • LeetCode——贪心算法总结
    贪心算法的主要的解题目的思路是: 860.柠檬水找零这道题算是生活中很常见的一道题,对于每一个顾客如果我们都有足够的零钱给他找零,那么就返回true,只要有一个顾客没有足够的零钱找给他就返回false。顾客只能有3种纸币,5元,10元,20元。我们要统计5元和10元的数量,20元的不需要统计,因为20......
  • #yyds干货盘点# LeetCode程序员面试金典:最接近的三数之和
    题目:给你一个长度为n的整数数组 nums 和一个目标值 target。请你从nums中选出三个整数,使它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在恰好一个解。 示例1:输入:nums=[-1,2,1,-4],target=1输出:2解释:与target最接近的和是2(-1+2+1=2)......
  • #yyds干货盘点# LeetCode面试题:二进制求和
    1.简述:给你两个二进制字符串a和b,以二进制字符串的形式返回它们的和。 示例 1:输入:a="11",b="1"输出:"100"示例 2:输入:a="1010",b="1011"输出:"10101"2.代码实现:classSolution{publicStringaddBinary(Stringa,Stringb){......