当前位置:网站首页>[leetcode] 33. Search rotation sort array
[leetcode] 33. Search rotation sort array
2022-07-26 15:22:00 【Xiaoqu classmate】
33、 Search rotation sort array
subject :
An array of integers nums In ascending order , The values in the array Different from each other .
Before passing it to a function ,nums In some unknown subscript k(0 <= k < nums.length) On the rotate , Make array [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] ( Subscript from 0 Start Count ).
for example , [0,1,2,4,5,6,7] In subscript 3 It may turn into [4,5,6,7,0,1,2] .
Here you are. After rotation Array of nums And an integer target , If nums There is a target value in target , Returns its subscript , Otherwise return to -1 .
You have to design a time complexity of O(log n) The algorithm to solve this problem .
Example 1:
Input :nums = [4,5,6,7,0,1,2], target = 0
Output :4
Example 2:
Input :nums = [4,5,6,7,0,1,2], target = 3
Output :-1
Example 3:
Input :nums = [1], target = 0
Output :-1
Tips :
1 <= nums.length <= 5000
-10^ 4 <= nums[i] <= 10^4
nums Each value in the unique
Topic data assurance nums Rotated on a previously unknown subscript
-10^ 4 <= target <= 10^4
Their thinking :
For ordered arrays , You can use the binary search method to find elements .
What we can find is that , When we split the array from the middle into left and right parts , There must be a part of the array that is ordered . Take for example , We from 6 After this position is separated, the array becomes [4, 5, 6] and [7, 0, 1, 2] Two parts , And the left side of it [4, 5, 6] The array of this part is ordered , So are others .
This suggests that we can view the current mid Two parts separated for the location of the partition [l, mid] and [mid + 1, r] Which part is orderly , And determine how we should change the upper and lower bounds of the binary search according to the ordered part , Because we can tell from the ordered part that target In not in this part :
If [l, mid - 1] It's an ordered array , And target The size of [nums[l],nums[mid]), We should narrow our search to [l, mid - 1], Otherwise, in the [mid + 1, r] Search for .

It should be noted that , There are many ways to write dichotomy , So in judgment target There may be differences in detail when the relationship between size and ordered parts .
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
if (n == 0) {
return -1;
}
if (n == 1) {
return nums[0] == target ? 0 : -1;
}
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 (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[n - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}

边栏推荐
- Is there any need for livedata to learn—— Jetpack series (2)
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the add parameter to add the mean and standard deviation vertical lines, and set the error.plo
- Unity URP entry practice
- VP视频结构化框架
- 什么是传输层协议TCP/UDP???
- 大学生如何申请实用新型专利?
- FOC学习笔记-坐标变换以及仿真验证
- 【基础】动态链接库/静态链接库的区别
- OpenGL learning diary 2 - shaders
- 食品制造企业想要实现智能协同的供应商管理,选择SRM供应商系统就够了
猜你喜欢

北京的大学排名

Parallel d-Pipeline: A Cuckoo Hashing Implementation for Increased Throughput论文总结
![[Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?](/img/5b/02a71384c62e998d40d6ce7a98082a.png)
[Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?

外文文献查找技巧方法有哪些

Cs224w (Figure machine learning) 2021 winter course learning notes 5

Deep Packet Inspection Using Cuckoo Filter论文总结
![[leetcode daily question] - 268. Missing numbers](/img/96/dcf18522257dea7cb7e52f5bb7ced3.png)
[leetcode daily question] - 268. Missing numbers

【基础】动态链接库/静态链接库的区别

Environment regulation system based on Internet of things (esp32-c3+onenet+ wechat applet)

【LeetCode】33、 搜索旋转排序数组
随机推荐
OpenGL学习日记2——着色器
Simulation of character function and string function
Deep Packet Inspection Using Cuckoo Filter论文总结
哪里有写毕业论文需要的外文文献?
81.(cesium之家)cesium修改灰色背景(默认蓝色)
蓝牙BLE4.0-HM-10设备配对指南
Which software must be used by scientific researchers to read literature?
DICOM学习资料收集
FOC learning notes - coordinate transformation and simulation verification
Data permissions should be designed like this, yyyds!
R language ggplot2 visualization: visual line graph, visual line graph for different groups using the group parameter in AES function
Solve the problem that typora pictures cannot be displayed
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用fitted函数计算出模型的拟合的y值(响应值)向量
Huawei applications have called the checkappupdate interface. Why is there no prompt for version update in the application
Abbkine EliKine人甲胎蛋白(AFP)ELISA定量试剂盒操作方法
Character function and string function and memory function
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
The practice of software R & D should start from the design
食品制造企业想要实现智能协同的供应商管理,选择SRM供应商系统就够了
Environment regulation system based on Internet of things (esp32-c3+onenet+ wechat applet)