当前位置:网站首页>81. 搜索旋转排序数组 II
81. 搜索旋转排序数组 II
2022-06-30 00:57:00 【Melody2050】
在经过一次旋转的有序数组中寻找给定值,并且数组中可能有重复元素。这里的难点在于可能有重复元素,导致旋转点位置的判断变得困难。如果没有重复元素,那么只需去比较left, mid, right三个位置的值,就能确定旋转点在哪个半边。
思路
1. 尝试寻找旋转点
首先,要尝试在这个数组中寻找旋转点,将整个数组分为左右半边,然后判断:
- 是否能确定旋转点在左半边?
- 是否能确定旋转点在右半边?
- 如果既不能确定在左半边/右半边,那么l++。
如何确定旋转点?记住一个窍门: 区间两端出现逆序时,代表旋转点在区间内部。
当nums[left] > nums[mid]时,一定旋转点在左半边,为什么呢?因为左半边出现了逆序,那么左侧至少有一个旋转点。而数组中至多有一个旋转点,所以右侧一定是顺序的。
同理,当nums[mid] > nums[right]时,一定旋转点在右半边。
为什么只有当出现端点与中点的逆序时,才能确定旋转点坐在半区?因为只有出现逆序,才能确定该区间有旋转点,如果
边栏推荐
- Swift notes
- 外包干了三年,废的一踏糊涂...
- 在线文本数字识别列表求和工具
- Small and medium-sized enterprises should pay attention to these points when signing ERP contracts
- Seata 與三大平臺攜手編程之夏,百萬獎金等你來拿
- [cloud native] kernel security in container scenario
- 网易云音乐内测音乐社交 App“MUS”,通过音乐匹配同频朋友
- Time does not spare
- How much is the fixed asset management system and the price of the fixed asset management system
- Time flies that year
猜你喜欢
![[deep learning compilation] operator compilation IR conversion](/img/10/bdcabde772e65eebc93f870f597524.png)
[deep learning compilation] operator compilation IR conversion
![[MySQL basic] general syntax 2](/img/fe/6837fe96cb99b54e5cbce8f20787a5.png)
[MySQL basic] general syntax 2

Nested call and chained access of functions in handwritten C language

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

Traffic, but no sales? 6 steps to increase website sales

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

阿于的彩虹桥

2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h

利用tsne将不同句子关于相似度可视化出来

Wechat applet - requestsubscribemessage:fail can only be invoked by user tap gesture
随机推荐
赛芯电子冲刺科创板上市:拟募资6.23亿元,共有64项专利申请信息
HC32M0+ GPIO
C语言课设心得之“推箱子”课设作品开源分享
Distributed task scheduling elasticjob demo
Comparison between strings localecompare
阿四的情绪波动
解决choice金融终端Excel/Wps插件修复visual basic异常
Yunna | fixed assets information system management, information-based fixed assets management
PHP wechat merchant transfer to change initiating merchant transfer API
Go out and protect yourself
latex如何输入一个矩阵
Exercise "product": self made colorful Prompt string display tool (for loop and if condition judgment)
Database learning notes (sql03)
[spark] basic Scala operations (continuous update)
浅析现代Web端im即时通讯开发技术
如何查看一个文件夹下所有文件的大小?
[qnx hypervisor 2.2 user manual]6.2.2 communication between guest and host
Wechat applet - requestsubscribemessage:fail can only be invoked by user tap gesture
HC32M0+ GPIO
Sfdp 超级表单开发平台 V6.0.4 正式发布