当前位置:网站首页>【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;
}
}
边栏推荐
- leetcode:221. 最大正方形【dp状态转移的精髓】
- [notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
- 时钟周期
- SAP UI5 ObjectPageLayout 控件使用方法分享
- Hiengine: comparable to the local cloud native memory database engine
- Rocky basics 1
- Small case of function transfer parameters
- Flutter InkWell & Ink组件
- 从外卖点单浅谈伪需求
猜你喜欢
聊聊异步编程的 7 种实现方式
Introduction aux contrôles de la page dynamique SAP ui5
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
CloudCompare——点云切片
Natural language processing series (I) introduction overview
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
“百度杯”CTF比赛 九月场,Web:SQL
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
Flutter draws animation effects of wave movement, curves and line graphs
随机推荐
RHCSA10
简单上手的页面请求和解析案例
SAP UI5 ObjectPageLayout 控件使用方法分享
Difference between avc1 and H264
MATLAB论文图表标准格式输出(干货)
go 数组与切片
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
Detailed explanation of navigation component of openharmony application development
C# 对象存储
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
Leetcode20. Valid parentheses
Principle and configuration of RSTP protocol
实现 1~number 之间,所有数字的加和
Go array and slice
AVC1与H264的区别
SAP UI5 DynamicPage 控件介绍
Actual combat simulation │ JWT login authentication
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Small case of function transfer parameters
LB10S-ASEMI整流桥LB10S