当前位置:网站首页>【Hot100】33. Search rotation sort array
【Hot100】33. Search rotation sort array
2022-07-05 13:14:00 【Wang Liuliu's it daily】
33. Search rotation sort array
Medium question
However, it is to find a number from an ordered sequence , First reaction Should be 「 Two points 」.
An originally ordered array rotates at a certain point , In fact, it is to divide the original ascending array into two segments .
「 Two points 」 The essence of is two-stage , Not monotonicity . As long as a paragraph satisfies a certain property , Another paragraph does not satisfy a certain property , You can use it 「 Two points 」.
Rotated array , Obviously, the first half meets >= nums[0], The latter half is not satisfied >= nums[0]. We can use this as a basis , adopt 「 Two points 」 Find the rotation point .
class Solution {
public int search(int[] nums, int target) {
// The length of the array
int n = nums.length;
// There are no elements in the array
if (n == 0) {
return -1;
}
// There is an element in the array
if (n == 1) {
return nums[0] == target ? 0 : -1;
}
// Define the left and right boundaries of binary search
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[0] <= nums[mid]) {
// If target stay [l,mid] Inner always reduces the right boundary
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
// target<nums[0] ,target>nums[mid] be not in [l,mid] Within the interval , Look to the right
l = mid + 1;
}
} else {
//nums[0] > nums[mid] [5,6,0,1,2,3,4]
if (nums[mid] < target && target <= nums[n - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}
边栏推荐
- Lb10s-asemi rectifier bridge lb10s
- Small case of function transfer parameters
- How can non-technical departments participate in Devops?
- 《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
- 946. Verify stack sequence
- ABAP editor in SAP segw transaction code
- Run, open circuit
- 同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
- Principle and configuration of RSTP protocol
- [deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
猜你喜欢

LeetCode20.有效的括号

简单上手的页面请求和解析案例

A specific example of ABAP type and EDM type mapping in SAP segw transaction code

Talk about seven ways to realize asynchronous programming

uni-app开发语音识别app,讲究的就是简单快速。

“百度杯”CTF比赛 九月场,Web:Upload

数据泄露怎么办?'华生·K'7招消灭安全威胁

解决uni-app配置页面、tabBar无效问题

Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function

STM32 and motor development (from architecture diagram to documentation)
随机推荐
Detailed explanation of navigation component of openharmony application development
国际自动机工程师学会(SAE International)战略投资几何伙伴
##无监控,不运维,以下是监控里常用的脚本监控
Get you started with Apache pseudo static configuration
SAP UI5 ObjectPageLayout 控件使用方法分享
碎片化知识管理工具Memos
How to realize batch sending when fishing
Introduction to sap ui5 flexiblecolumnlayout control
阿里云SLB负载均衡产品基本概念与购买流程
Didi open source Delta: AI developers can easily train natural language models
逆波兰表达式
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
Talking about fake demand from takeout order
RHCSA10
使用Dom4j解析XML
Pycharm installation third party library diagram
Hiengine: comparable to the local cloud native memory database engine
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table