[Leetcode] 81. Search in Rotated Sorted Array II 解题报告

MATLAB AppDesigner 中TextArea保留原有信息并换行显示提示信息 App Designer 中的文本区域TextArea组件,一般可以用来作为matlab app设计中的信息显示。 app.TextArea.Value = 'Hello Matlab'; Value可以是字符向量、字符向量元胞数组、字符串数组或一维分类数组。这样直接给TextArea赋值,每次赋值都会消除先前的信息,然后显示新的信息。如果想要在app实现保留原有信息、多行信息的输出,在MathWorks官方帮助中心给出了一种方法: cellArrayText{1} = sprintf('%s\n 阅读详情

题目:

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Write a function to determine if a given target is in the array.

The array may contain duplicates.

思路:

1、先利用O(n)的时间复杂度扫描数组,得到pivot的索引,然后分别在pivot的左边和右边利用二分法进行查找。这种方法的时间复杂度确定为O(n)。

2、设计适用于旋转有序数组的二分法。每次二分有四种情况需要考虑:

      1)中间的数等于target,那么直接返回结果;

      2)中间的数小于最右边的数,这说明右边区间是递增的区间,然后判断target是否在这个区间之内:

            a)如果target的大小介于中间的数和最右边的数,那么说明target如果存在则一定会在右边区间,此时更新左边界;

            b)否则target只可能存在于左边区间中,则更新右边界;

      3)中间的数大于最右边的数,这说明左区间是递增区间,然后判断target是否在这个区间之中:

            a)如果target的大小介于最左边的数和中间的数,说明target如果存在则一定会在左边区间,此时更新右边界;

            b)否则target只能存在于右边区间中,则更新左边界。

      4)中间的数等于最右边的数。这种情况无法确定要搜索哪一边。此时只能采用最保守的方法:抛弃最右边的数。

      在最坏情况下,思路2的时间复杂度会退化到O(n),但通常情况下其时间复杂度是O(logn)。

      常规二分查找法往往都是比较中间数和target的大小,但是对于旋转数组而言,恰恰不能用这种方法,因为即使我们比较了,也不知道接下来往哪个方向走。一个比较好的办法是比较中间数和边界数的大小,这样可以首先确定哪个区间是严格递增的,然后再根据子区间是否单调递增,采用常规二分查找,或者递归调用本函数。

代码:

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        if (nums.size() == 0) {
            return false;
        }
        return binarySearch(nums, target, 0, nums.size() - 1);
    }
private:
    bool binarySearch(vector<int> &nums, int target, int left, int right) {
        if (left > right) {
            return false;
        }
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) {
            return true;
        }
        else if (nums[mid] < nums[right]) {     // right side is increasing
            if (nums[mid] < target && target <= nums[right]) {
                return binarySearch(nums, target, left + 1, right);
            }
            else {
                return binarySearch(nums, target, left, right - 1);
            }
        }
        else if (nums[mid] > nums[right]) {     // left side is increasing
            if (nums[left] <= target && target < nums[mid]) {
                return binarySearch(nums, target, left, right - 1);
            }
            else {
                return binarySearch(nums, target, left + 1, right);
            }
        }
        else {
            return binarySearch(nums, target, left, right - 1);
        }
    }
};

电芯外观及体系设计(Cell Design)-①容量平衡设计(Cell Balance Design) 全面获取并正确解读客户需求,将需求进行分解和预判,结合公司现有资源、技术,给出设计方案。一个优秀的电芯设计方案,应该是稳健的,可操作的,满足客户需求的同时,并能为公司带来较多利润。电芯设计需同时考虑周期,性能,品质,加工性,成本,及安全与稳健性。设计人员时刻要有成本控制观念,但利润则是由公司战略所决定。 阅读详情

相关推荐

Spring Boot 实现多图片上传并回显,涨姿势了~

上传 Controller的代码非常简单,由于用了SpringMVC框架,所以直接用MultipartFile来接即可。由于是多图片上传所以用数组来接。此处应该注意参数名应该和<input>中的name值相对应: @RequestMapping("/pic") @ResponseBody publicResponseEntity<String>pic(MultipartFile[]pictures)throwsException{ ResponseEnti...

weixin_54556126的博客 893

[leetcode] 81. Search in Rotated Sorted Array II 解题报告

题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity

小榕流光的专栏 743

Conda环境创建报错?教你快速解决Solving environment: failed和ResolvePackageNotFound问题

本文深入解析Conda环境创建中常见的'Solving environment: failed'和'ResolvePackageNotFound'报错原因,并提供从基础到高级的系统化解决方案。涵盖渠道配置、版本通配、环境文件编辑等实用技巧,帮助开发者快速解决conda环境创建问题,提升工作效率。

weixin_28677451的博客 258

81. Search in Rotated Sorted Array II【leetcode解题报告】

Description Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]). You are given a target value to sear...

tan_change的专栏 262

LSTM模型 轨迹经纬度预测

原始时间序列数据如下所示,我们只取前两列纬度和经度作为输入数据 我们设定用前六个位置信息预测下一个位置,则两个样本的输入输出数据如下所示: 创建LSTM网络训练模型: import numpy as np from keras.layers.core import Dense, Activation, Dropout from keras.layers import LS...

Muzi_Water的博客 2万+

双目相机立体匹配算法概述(含Python代码)

立体匹配任务是计算机视觉中的一个重要问题,旨在从一对具有相同场景的图像中估计每个像素的视差(disparity)。本文对现有的传统算法和深度学习算法进行了综述,以便于各位选择合适的立体匹配算法用于深度估计或三维建模。

m0_59257547的博客 8291

【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

题目分析: 这一题是给定一个按照升序排序的数组在预先未知的某个点上进行了旋转。( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] )。要求编写一个函数来判断给定的目标值是否存在于数组中。若存在返回 true,否则返回 false。如: 输入: nums = [2,5,6,0,0,1,2], target = 0 输出: true 解题思路: 这个题和【Leet...

L141210113的专栏 345

Python全栈入门到实战【进阶篇 1】类与对象:面向对象编程的基石

Python中定义类使用**class关键字**,创建对象使用**类名()**(类似调用函数),语法简洁,我们从最基础的空类开始,逐步拆解。class# 定义空的学生类,pass表示占位,无实际内容 class Student : pass编码规范:类名遵循大驼峰命名法(每个单词首字母大写,无下划线),如StudentCarPhone,这是Python官方推荐的PEP8规范,便于区分类和其他标识符。实例方法必须定义在类内部;实例方法的第一个参数必须是self(名称可自定义,如this。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 3403

Leetcode 81. Search in Rotated Sorted Array II 旋转数组查询2 解题报告

1 解题思想基本思想和之前的一样,但是因为允许重复,如果出现无法判断的情况(请看代码里 新增的区分 的注释),则需要缩减一个范围继续搜索具体的请看第一个问题: Leetcode #33. Search in Rotated Sorted Array 旋转数组查询 解题报告2 原题Follow up for “Search in Rotated Sorted Array”: What if du

MebiuW的专栏 551

VSCode + QMT 量化交易完整解决方案!

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1327

Autosar MCAL-S32K324 Fls配置-基于EB

Autosar MCAL Flash配置,基于S32K324,EB软件

赞的博客 1513

[leetcode] 33. Search in Rotated Sorted Array 解题报告

题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2)

小榕流光的专栏 2034

华为OD机试真题 新系统【优化充电桩调度算法】

华为OD机试 新系统真题【优化充电桩调度算法】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【优化充电桩调度算法】

qq_45776114的博客 753

【LeetCode】解题81:Search in Rotated Sorted Array II

LeetCode解题 81:Search in Rotated Sorted Array II Problem 81: Search in Rotated Sorted Array II [Medium]解题思路Solution (Java) Problem 81: Search in Rotated Sorted Array II [Medium] Suppose an array sorted...

Fayedily的博客 255

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

leetcode 81 Search in Rotated Sorted Array II 二分循环变种

Follow up for "Search in Rotated Sorted Array": What ifduplicatesare allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the a...

taoqick的专栏 1056

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode 81. Search in Rotated Sorted Array II(二分)

Search in Rotated Sorted Array II Medium Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]). You are gi...

da_kao_la的博客 203

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

[leetcode 81] Search in Rotated Sorted Array II

题目描述: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Write a function to determine if a given targe

新博客地址:www.yizhen-blog.com 欢迎交流 769

LeetCode81——Search in Rotated Sorted Array II

LeetCode81——Search in Rotated Sorted Array II

上善若水 1170

LeetCode 81,在不满足二分的数组内使用二分法 II

我们来聊聊LeetCode中的81题Search in Rotated Sorted ArrayII。 它的官方难度是Medium,点赞1251,反对470,通过率32.8%。从通过率上来看,这题属于Medium难度当中偏难一些的题目,也的确如此,稍稍有些考验思维。 题意 假设我们有一个含有重复元素的有序数组,我们随意选择一个位置将它分成两半,然后将这两个部分调换顺序拼接成一个新的数组。现在给定一个target,要求返回一个bool结果,表明target是否在数组当中。 样例 Input: nu

xueqilily的博客 224

Leetcode 81. Search in Rotated Sorted Array II 搜索旋转排序数组 II

Leetcode 81. Search in Rotated Sorted Array II 搜索旋转排序数组 II 标签: Leetcode 题目地址: https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/ 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,0,1...

CoderWangSon 245

leetcode【81】Search in Rotated Sorted Array II

问题描述: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,[0,0,1,2,2,5,6]might become[2,5,6,0,0,1,2]). You are given a target value to search. If...

fanyuwgy的博客 205

Leetcode 81. Search in Rotated Sorted Array II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]). You are given a target value to search. If found ...

salmonwilliam的博客 389

【LeetCode】 81. Search in Rotated Sorted Array II 搜索旋转排序数组 II(Medium)(JAVA)

【LeetCode】 81. Search in Rotated Sorted Array II 搜索旋转排序数组 II(Medium)(JAVA) 题目地址: https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题目描述: Suppose an array sorted in ascending order is ro...

qq_16927853的博客 252

LeetCode-81. Search in Rotated Sorted Array II [C++][Java]

There is an integer arraynumssorted in non-decreasing order (not necessarily withdistinctvalues). Given the arraynumsafterthe rotation and an integertarget, returntrueiftargetis innums, orfalseif it is not innums.

贫道绝缘子的博客 240

leetcode-81-Search in Rotated Sorted Array II 查找

问题题目:[leetcode-81]思路跟[leetcode-33]相比,主要是重复元素的问题。举两个例子: [3,5,5,5,1]和[5,1,1,1,3],对于这种情况,算法的其余部分基本不变。首先,是pivot的查找问题。 我做如下定义,如果一个序列存在一个升序对和一个降序对,那么我认为这个序列而存在一个逆序。那么现在的问题就转换为判断一个序列中是否存在一个逆序对。当前问题与之前问题的区别是

Kang_TJU的博客 495

LeetCode81 Search in Rotated Sorted Array II

LeetCode81 Search in Rotated Sorted Array II问题描述 Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why? Sup

yanqinghe123的专栏 252
上一篇: [Leetcode] 80. Remove Duplicates from Sorted Array II 解题报告
下一篇: [Leetcode] 82. Remove Duplicates from Sorted List II 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值