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

Leetcode81. 搜索旋转排序数组 II

时间:2023-03-28 20:55:15浏览次数:34  
标签:return target nums int mid Leetcode81 II 排序 check

class Solution {
public:
    bool check(vector<int> &nums,int target,int l,int r)//[l,r]区间查找target
    {
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(target>=nums[mid])   l=mid;
            else r=mid-1;
        }
        return nums[l]==target;
    }
    bool search(vector<int>& nums, int target) {
        int n=nums.size()-1;
        while(n&&nums[n]==nums[0])  n--;
        if(nums[n]>nums[0]) //说明只有一段单调的
            return check(nums,target,0,n);
        int l=0,r=n;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(nums[mid]>=nums[0])   l=mid+1;
            else r=mid;
        }
        //根据target与nums[0]的关系,判断二分区间
        if(target>=nums[0])  return check(nums,target,0,l-1);
        else return check(nums,target,l,n);
    }
};

标签:return,target,nums,int,mid,Leetcode81,II,排序,check
From: https://www.cnblogs.com/tangxibomb/p/17266638.html

相关文章

  • 面试题59 - II. 队列的最大值(剑指offer)
    题目描述:请定义一个队列并实现函数max_value得到队列里的最大值,要求函数max_value、push_back和pop_front的均摊时间复杂度都是O(1)。若队列为空,pop_front和max_v......
  • 练习——简易的冒泡排序
    packagecom.q1u.array;importjava.util.Arrays;//冒泡排序//1.比较数组中两个相邻的元素,如果第一个数大于第二个,交换两者位置//2.每一次比较,都会产生一个最大或者......
  • 确定比赛名次 HDU - 1285 (拓扑排序)
    题意:有N个比赛队(1≤N≤500),编号依次为1,2,3,...,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比......
  • 老是忘记的字典排序
    amount_total=0forsubscription_type,product_infoinbill_group_dict.items():consume_group_doc_lst["subscription_type"]=subscription_typeconsume_gr......
  • 选择排序
    欢迎关注fish的公众号:fish码农成长之旅相信大家对扑克牌并不陌生,当我们在齐牌的时候是不是会按照大小顺序进行排列,选择排序的过程就跟扑克牌差不多一样的直观简单。其......
  • ascii-gb-unicode-utf-8
    中国人民通过对ASCII编码的中文扩充改造,产生了GB2312编码,可以表示6000多个常用汉字。汉字实在是太多了,包括繁体和各种字符,于是产生了GBK编码,它包括了GB2312中的编......
  • 代码随想录day 28 93.复原IP地址 | 78.子集 | 90.子集II
    给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式。有效的IP地址正好由四个整数(每个整数位于0到255之间组成,且不能含有前导0),整数之间用'.'分隔。......
  • Java学习----冒泡排序
    冒泡排序importjava.util.Arrays;publicclassMaoPaoPaiXu{publicstaticvoidmain(String[]args){int[]a={1,2,3,5,7,9,22,44,63,75};......
  • day27 打卡39. 组合总和 40.组合总和II 131.分割回文串
    day27打卡39.组合总和40.组合总和II131.分割回文串39.组合总和39题目链接classSolution{List<List<Integer>>result=newArrayList<>();LinkedList......
  • 吃巧克力,容器vector、map,容器适配器 priority_queue,算法sort排序
     #include<algorithm>#include<queue>#include<map>#include<vector>#include<iostream>usingnamespacestd;structchocolate{longlonga;//价......