当前位置:网站首页>81. 搜索旋转排序数组 II
81. 搜索旋转排序数组 II
2022-06-30 00:57:00 【Melody2050】
在经过一次旋转的有序数组中寻找给定值,并且数组中可能有重复元素。这里的难点在于可能有重复元素,导致旋转点位置的判断变得困难。如果没有重复元素,那么只需去比较left, mid, right三个位置的值,就能确定旋转点在哪个半边。
思路
1. 尝试寻找旋转点
首先,要尝试在这个数组中寻找旋转点,将整个数组分为左右半边,然后判断:
- 是否能确定旋转点在左半边?
- 是否能确定旋转点在右半边?
- 如果既不能确定在左半边/右半边,那么l++。
如何确定旋转点?记住一个窍门: 区间两端出现逆序时,代表旋转点在区间内部。
当nums[left] > nums[mid]时,一定旋转点在左半边,为什么呢?因为左半边出现了逆序,那么左侧至少有一个旋转点。而数组中至多有一个旋转点,所以右侧一定是顺序的。
同理,当nums[mid] > nums[right]时,一定旋转点在右半边。
为什么只有当出现端点与中点的逆序时,才能确定旋转点坐在半区?因为只有出现逆序,才能确定该区间有旋转点,如果
边栏推荐
- 作文总写不好怎么办?猿辅导:家长要注意这几点
- CSV文件格式——方便好用个头最小的数据传递方式
- After the element uses align items center and overflow auto, some contents are not fully displayed
- Visual Studio 2017 无法打开包括文件: “QOpenGLFunctions_3_3_Core”: No such file or directory
- 2022-06-29: x = {a, B, C, D}, y = {e, F, G, H}, the length of the two small arrays X and Y is 4. If yes: a + e = B + F = C + G = D + H
- How to view the size of all files in a folder?
- Swift notes
- Ml: introduction to confidence interval (the difference and relationship between precision / accuracy / accuracy), use method, and detailed introduction to case application
- 阿洛觉得自己迷茫
- [cloud native] kernel security in container scenario
猜你喜欢

Yunna | fixed assets information system management, information-based fixed assets management

Practical application of information security

Crmeb SMS for program configuration of knowledge payment system

岁月不饶人

Equivalence class partition method for test case design method

How latex enters a matrix

开发者,为什么说容器技术的成熟预示着云原生时代的到来?

Using tsne to visualize the similarity of different sentences

SFDP super form development platform v6.0.4 was officially released

UDP servers and clients in go
随机推荐
Common settings in idea
【Proteus仿真】8位端口检测8独立按键
2022年最新最详细IDEA关联数据库方式、在IDEA中进行数据库的可视化操作(包含图解过程)
浅析现代Web端im即时通讯开发技术
The SQL statement concat cannot find the result
外包干了三年,废的一踏糊涂...
作文总写不好怎么办?猿辅导:家长要注意这几点
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
[MRCTF2020]Ezpop-1|php序列化
Interviewer: how to solve the problem of massive requests for data that does not exist in redis, which affects the database?
Transaction summary on June 25, 2022
C语言课设心得之“推箱子”课设作品开源分享
Equivalence class partition method for test case design method
清洁、对话、带娃,扫地机摆脱“人工智障”标签
Traffic, but no sales? 6 steps to increase website sales
利用huggingface进行文本分类
[qnx hypervisor 2.2 user manual]6.2.2 communication between guest and host
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
解决choice金融终端Excel/Wps插件修复visual basic异常