当前位置:网站首页>81. 搜索旋转排序数组 II
81. 搜索旋转排序数组 II
2022-06-30 00:57:00 【Melody2050】
在经过一次旋转的有序数组中寻找给定值,并且数组中可能有重复元素。这里的难点在于可能有重复元素,导致旋转点位置的判断变得困难。如果没有重复元素,那么只需去比较left, mid, right三个位置的值,就能确定旋转点在哪个半边。
思路
1. 尝试寻找旋转点
首先,要尝试在这个数组中寻找旋转点,将整个数组分为左右半边,然后判断:
- 是否能确定旋转点在左半边?
- 是否能确定旋转点在右半边?
- 如果既不能确定在左半边/右半边,那么l++。
如何确定旋转点?记住一个窍门: 区间两端出现逆序时,代表旋转点在区间内部。
当nums[left] > nums[mid]时,一定旋转点在左半边,为什么呢?因为左半边出现了逆序,那么左侧至少有一个旋转点。而数组中至多有一个旋转点,所以右侧一定是顺序的。
同理,当nums[mid] > nums[right]时,一定旋转点在右半边。
为什么只有当出现端点与中点的逆序时,才能确定旋转点坐在半区?因为只有出现逆序,才能确定该区间有旋转点,如果
边栏推荐
- 外包干了三年,废的一踏糊涂...
- [MRCTF2020]Ezpop-1|php序列化
- MySQL deadlock
- 网易云音乐内测音乐社交 App“MUS”,通过音乐匹配同频朋友
- Clean, talk, bring children, and get rid of the label of "artificial mental retardation" for the sweeper
- @Bugs caused by improper use of configurationproperties
- VIM editor common instructions
- 解决choice金融终端Excel/Wps插件修复visual basic异常
- Arlo felt lost
- IDEA中的常用设置
猜你喜欢

网易云音乐内测音乐社交 App“MUS”,通过音乐匹配同频朋友

Which department should the company's fixed assets be managed? How should the company's fixed assets be managed

Cloud, IPv6 and all-optical network

A Yu's Rainbow Bridge

【Games101】Transformation

如何做好测试用例设计

Good test / development programmers vs. average programmers

Time does not spare

Online text digit recognition list summation tool

I / o initial et son fonctionnement de base
随机推荐
Which direction of network development is better? Data communication engineer learning path sharing
我,33岁,字节跳动测试开发,揭开北京“测试岗”的真实收入
太卷了~ 八股文,面试最强王者!
利用tsne将不同句子关于相似度可视化出来
Outsourcing for 3 years is a waste
If the amount exceeds 6 digits after the decimal point, only 6 digits will be reserved, and if it is less than 6 digits, it will remain the same - Basic accumulation
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
视频转图像-cv2.VideoCapture()用法
Comment personnaliser les modèles et générer rapidement le code complet dans l'idée?
How much is the fixed asset management system and the price of the fixed asset management system
Wechat applet - requestsubscribemessage:fail can only be invoked by user tap gesture
字符串之间的比较之 localeCompare
简单的页面
[three.js] Web3D first experience
Flask web minimalist tutorial (III) - Sqlalchemy (part a)
[proteus simulation] 8-bit port detection 8 independent keys
外包干了三年,废的一踏糊涂...
A Yu's Rainbow Bridge
传统微服务框架如何无缝过渡到服务网格 ASM
干外包3年,真废了...