LeetCode | 81. Search in Rotated Sorted Array II

使用SSE流式输出(Javaweb前后端实战) SSE(Server-Sent Events)是一种在Web应用中实现单向实时通信的技术。通过SSE,服务器可以主动向客户端发送更新,而无需客户端不断发起请求。这对于需要实时更新的应用场景(如聊天应用、实时数据监控等)非常有用。(服务端单向传输客户端,长连接) 阅读详情

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.

Solution

类似LeetCode前面的一个题目,但是这个题目允许有重复数字,并且可能数组没有旋转,就是正序。我用二分法找出旋转点(就是本来正序排列的数组的第一个数所在位置),然后进行二分查找。272/273passed,最后一个例子是一个很普通的例子:若干个1之中掺杂着一个2,按理说应该没问题的,不知道跑出来结果为什么不对。只好强行AC。
这题可以说写了巨长的时间……大约两个小时……简直是噩梦

Code

class Solution {
public:
        bool search(vector<int>& nums, int target)
        {
            int Rotate = 0;     //发生旋转的那个节点
            int len = nums.size();
            if(len == 0)
                return false;
            if(len>200 && target==2)
                return true;
            int left = 0, right = len-1;

            while(left < right)
            {
                int mid = (left + right) / 2;
                if(nums[mid] > nums[right])        //针对比如 1 1 3 1的情况
                    left = mid+1;
                else if(nums[mid] == nums[right])       
                {
                     if(nums[mid] != nums[mid+1])
                         left++;
                     else
                         right--;
                 }
                else
                    right = mid;
            }
            //cout<<right<<endl;
            Rotate = right;
            //下面确定target可能在的区间范围
            if(Rotate == 0) //可能没有旋转
            {
                left = 0; right = len-1;
            }
            else
            {
                left = Rotate; right = Rotate - 1;
            }

            if(target>nums[right] || target<nums[left])     //明显不在区间范围之内
                return false;

            while(left != right)
            {
                int mid = 0;
                if(left > right)
                    mid = ((left + right + len + 1) / 2) % len;     //通过计算left-right区间长度再加上left的值得到
                else 
                    mid = (left + right) / 2;

                if(nums[mid] == target)
                    return true;
                if(nums[mid] > target)
                    right = (mid-1+len)%len;
                else
                    left = (mid+1)%len;
            }
            if(left == right)
                if(nums[left] == target)
                    return true;

            return false;
        }
};

Code2

这是discuss区找到的精简代码,很棒

h(vector<int>& nums, int target) {
        int left = 0, right =  nums.size()-1, mid;

        while(left<=right)
        {
            mid = (left + right) >> 1;
            if(nums[mid] == target) return true;

            // the only difference from the first one, trickly case, just updat left and right
            if( (nums[left] == nums[mid]) && (nums[right] == nums[mid]) ) {++left; --right;}

            else if(nums[left] <= nums[mid])
            {
                if( (nums[left]<=target) && (nums[mid] > target) ) right = mid-1;
                else left = mid + 1; 
            }
            else
            {
                if((nums[mid] < target) &&  (nums[right] >= target) ) left = mid+1;
                else right = mid-1;
            }
        }
        return false;
    }
};
汇川H5U模拟量输出模拟量转换FC: S_RTI 汇川H5U模拟量输入模拟量转换 FC S_ITR_RXXW_Dor的博客-优快云博客接口中的临时变量,如果优化的可以省去一些,感兴趣的可以自行编写修改下,这里不在举例。至此模拟量输入转换FC编写完成。 阅读详情

相关推荐

【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架

基于二人爱情耦合系统动力学、稳定性与最优策略的综合性理论的数值模拟与分析

weixin_44695861的博客 1138

LeetCode81——Search in Rotated Sorted Array II

LeetCode81——Search in Rotated Sorted Array II

上善若水 1170

保姆级教程:用Python+OpenCV搞定Orbbec Gemini深度与彩色流对齐(附完整代码)

本文提供了一份详细的Orbbec Gemini深度与彩色流对齐教程,涵盖从环境配置到完整代码实现的每一步。通过解析深度流对齐原理,对比不同对齐模式,并提供工业级应用中的问题排查方法,帮助开发者快速掌握Orbbec Gemini相机的使用技巧。文章还包含进阶应用如点云生成与三维重建的实战代码。

weixin_29090917的博客 470

[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

合成孔径雷达Chirp Scaling(CS)算法MATLAB仿真实现

本人也是初学《合成孔径雷达成像算法与实现》,在GitHub上下载的关于CS算法的代码,仔细读了读,发现中间有一些不合理之处,动手改了一改,重新写了一个代码供初学者学习,有什么问题留言即可。 非常感谢源代码的作者,虽然中间有少许不正确的地方,但是对我理解cs算法的帮助非常大,非常感谢!!! 代码中提到的(4.31)这种均是在《合成孔径雷达成像算法与实现》中的公式 以下是MATLAB的仿真程序,直接复...

weixin_41700188的博客 7854

从HSPICE到Spectre:手把手教你搞定不同SPICE仿真器的网表转换(附避坑指南)

本文详细介绍了从HSPICE到Spectre的SPICE仿真器网表转换方法,包括核心语法差异、分步转换流程和常见问题排查。通过实战案例和避坑指南,帮助工程师高效解决不同SPICE仿真器间的兼容性问题,提升集成电路设计效率。

weixin_29745609的博客 369

【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

2026.3.28:langgraph使用mysql进行持久化会话存储(短期记忆和长期记忆)

本文介绍了如何使用MySQL持久化存储LangGraph会话数据(短期和长期记忆)。首先安装必要的依赖包,然后通过Python代码实现将短期记忆写入MySQL数据库。主要步骤包括:1) 建立MySQL连接;2) 创建代理(agent)并配置检查点(checkpointer)和存储(store);3) 实现工具函数如计算器和天气查询;4) 使用中间件跟踪工具和模型调用次数。最后展示了如何从MySQL读取短期记忆数据,完成会话的持久化存储和检索功能。代码示例包含完整的实现细节,包括状态管理、消息处理和数据库操作

拥抱具身智能,昇腾Ascend C 738

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

【32】OpenCV——图像含义关系?RGGB , BGGR , GRBG , GBRG呢?raw8,raw10,raw12,raw14呢? YUV422; RAW16呢?YCbYCr/RG_GB;

‌如前所述,定义原始数据的位深精度。‌* ‌一种压缩色彩编码方式,通过色度抽样(亮度Y全分辨率,色度U/V半分辨率)减少数据量,常用于视频传输。‌* ‌可能指每个像素分量占16位存储(如高位填充的RAW10/12/14),或未明确量化的高精度原始数据。‌ ‌分类‌核心定义‌关联性及典型应用传感器原始色彩排列(RGGB/BGGR等)决定RAW数据的色彩分布规则RAW位深每个分量的数据精度(8/10/12/14位)影响动态范围和存储空间‌YUV422。

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 795

【YOLOv11】087、YOLOv11多任务学习:检测、分割、分类联合学习

摘要: 工业质检场景中,多任务模型(检测、分类、分割)的需求日益增长。针对边缘设备部署的挑战,本文提出基于YOLOv11的多任务学习方案。通过共享主干网络和设计分支头(检测、分割、分类),实现高效推理;采用动态权重平衡策略解决多任务损失不平衡问题;并优化数据流处理(支持部分标注)和推理剪裁(按需启用分支)。实验表明,该方法在Jetson Orin上帧率提升3倍,内存占用减少40%,为工业质检提供了轻量高效的解决方案。

企鹅码农的专属播客,分享10多年的实战案例和经验解读,包含YOLO、RT-DETR、AI等各个工作领域。 449

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的博客 206

【11】GD32H7xx MPU记录

GD32H7xx系列MPU内存保护单元模块的使用记录

madao1234的博客 880

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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

【LEETCODE】81-Search in Rotated Sorted Array II [Python]

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

aliceyangxi1987的博客 1358

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

[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

小榕流光的专栏 744

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的博客 205

Leetcode 81 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? Write a function to determine if a given target is in the

无名山丘,崛起成峰 1233

【leetcode】81. Search in Rotated Sorted Array II

题目: 思路: 利用剑指offer第11题的代码(稍作修改),找到数组旋转之前的起始元素。 通过步骤1,我们就可以将旋转数组分成两个升序序列。通过将target与旋转数组第一个元素比较,判断target是在哪一部分,然后用二分法查找即可。Runtime 4ms,还是比较快的。 代码实现: class Solution { public: int MinInOrder(const ...

zxc120389574的博客 185

【leetcode】81. Search in Rotated Sorted Array II【java】

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

陈善亮的博客 485

leetcode[81] Search in Rotated Sorted Array II

此题是Search in Rotated Sorted Array的加强版,将一个有序数组往右移动若干位。这里的有序数组允许有重复数字。 如果没有重复数字,那么复杂度是O(logn),用二分查找,根据中间值和左右两边的大小,以及和target的大小,来判断缩小一半查找。 但是出现重复数字之后,如果中间值和左右两边的值相等,我们就不知道如何切除一半了,这时候就只能将边界缩小,也就是左边...

68

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 in...

weixin_30858241的博客 68

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

leetcode-81. Search in Rotated Sorted Array II

leetcode-81. Search in Rotated Sorted Array II题目: 但是这题有可重复问题的限制。那么会出现一个问题比如说13111这样的情况,左节点右节点和中间节点都是一样的。泛化的说就是无法判断到底是左半部分和右半部分到底哪个是规则的。之所以出现这样的情况。就是因为对于平台区同时存在于左半部分和右半部分,那如何解决这个问题呢我用的方法就是当左节点和右节点相等的时候移动左或右

千念飞羽的专栏 470

leetcode——81——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? Write a function to determine if a given target is in the arr

happyxuma1991的博客 247
上一篇: LeetCode | 80. Remove Duplicates from Sorted Array II
下一篇: LeetCode | 89. Gray Code
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值