当前位置:网站首页>81. 搜索旋转排序数组 II
81. 搜索旋转排序数组 II
2022-06-30 00:57:00 【Melody2050】
在经过一次旋转的有序数组中寻找给定值,并且数组中可能有重复元素。这里的难点在于可能有重复元素,导致旋转点位置的判断变得困难。如果没有重复元素,那么只需去比较left, mid, right三个位置的值,就能确定旋转点在哪个半边。
思路
1. 尝试寻找旋转点
首先,要尝试在这个数组中寻找旋转点,将整个数组分为左右半边,然后判断:
- 是否能确定旋转点在左半边?
- 是否能确定旋转点在右半边?
- 如果既不能确定在左半边/右半边,那么l++。
如何确定旋转点?记住一个窍门: 区间两端出现逆序时,代表旋转点在区间内部。
当nums[left] > nums[mid]时,一定旋转点在左半边,为什么呢?因为左半边出现了逆序,那么左侧至少有一个旋转点。而数组中至多有一个旋转点,所以右侧一定是顺序的。
同理,当nums[mid] > nums[right]时,一定旋转点在右半边。
为什么只有当出现端点与中点的逆序时,才能确定旋转点坐在半区?因为只有出现逆序,才能确定该区间有旋转点,如果
边栏推荐
- IDEA中的常用设置
- HC32M0+ GPIO
- CSV文件格式——方便好用个头最小的数据传递方式
- 2022 6 月25 日交易总结
- [spark] basic Scala operations (continuous update)
- YuMinHong: my retreat and advance; The five best software architecture patterns that architects must understand; Redis kills 52 consecutive questions | manong weekly VIP member exclusive email weekly
- Which department should the company's fixed assets be managed? How should the company's fixed assets be managed
- How to customize templates and quickly generate complete code in idea?
- Some thoughts on life
- php微信商家转账到零钱 发起商家转账API
猜你喜欢

Netease cloud music beta music social app "mus" matches friends of the same frequency through music

如何在IDEA中自定義模板、快速生成完整的代碼?

数字垃圾是什么?跟随世界第一位AI艺术家,探索元碳艺术

Cloud, IPv6 and all-optical network

练习副“产品”:自制七彩提示字符串展示工具(for循环、if条件判断)

How did the data center change from "Britney Spears" to "Mrs. bull"?

Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business

Outsourcing for 3 years is a waste

【云原生】容器场景下的内核安全

【Games101】Transformation
随机推荐
After the element uses align items center and overflow auto, some contents are not fully displayed
Database learning notes (sql03)
I / o initial et son fonctionnement de base
[recommended] how to quickly locate a bug during testing
【mysql篇-基础篇】通用语法2
2022 6 月25 日交易总结
岁月不饶人
Go out and protect yourself
UDP servers and clients in go
十大券商有哪些?另外,手机开户安全么?
传统微服务框架如何无缝过渡到服务网格 ASM
[proteus simulation] 8-bit port detection 8 independent keys
Ml: introduction to confidence interval (the difference and relationship between precision / accuracy / accuracy), use method, and detailed introduction to case application
A Si's mood swings
Interviewer: how to solve the problem of massive requests for data that does not exist in redis, which affects the database?
Exercise "product": self made colorful Prompt string display tool (for loop and if condition judgment)
Too voluminous ~ eight part essay, the strongest king of interview!
数据中台咋就从“小甜甜”变成了“牛夫人”?
CSV文件格式——方便好用个头最小的数据传递方式
2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h