当前位置:网站首页>[notes] search rotation sort array
[notes] search rotation sort array
2022-07-25 07:14:00 【Qihai】
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
-104 <= nums[i] <= 104
nums Each value in the unique
Topic data assurance nums Rotated on a previously unknown subscript
-104 <= target <= 104source : Power button (LeetCode)
link :https://leetcode.cn/problems/search-in-rotated-sorted-array
The first thought is to use dichotomy to find . But dichotomy has a very fatal requirement -- That is, the selected area must be orderly . So how to find the above rotation array ?--> still Use dichotomy .
solution :
We can find out , Use dichotomy to find the right , Divide a rotation array into 2 after , There must be some Orderly . such as :[5 6 7 8 0 1 2 3 4] --> [5 6 7 8 0] [1 2 3 4] Then continue to divide in no order --> [5 6 7] [8 0] -> [8] [0]...
You can find , There will be a point in order every time , Until it is finally divided into two numbers .
Then find the orderly , Is it possible to use dichotomy to find ? The answer is . For example, find 3, Then after the first split , First judge which side is orderly , If it is ordered and satisfies the number found in this region , that , You can compress the region into an ordered one for binary search . If any condition is not met ( Not ordered or within its interval ), Then repeat the first step , Divide in disorder .
therefore , We can first distinguish 0 Subscripts and mid Whether the subscript area is orderly , Order goes into this . Judge whether the target value is within , It is no longer located in the disordered area . Otherwise, the second section is orderly , Repeat the above steps , Until the end . Find it and return its subscript . The end of the cycle means that it is not found , return -1.
Of course, the special case is to pass in an empty array or only one number , Judge alone .
The code is as follows :
int search(int* nums, int numsSize, int target){
if (numsSize == 0)
return -1;
if (nums[0] == target)
return 0;
int begin = 0;
int end = numsSize - 1;
while(begin <= end)
{
int mid = begin + (end - begin) / 2;
if (nums[mid] == target)
return mid;
if (nums[0] <= nums[mid])
{
if (target < nums[mid] && target >= nums[0])
end = mid - 1;
else
begin = mid + 1;
}
else
{
if (target <= nums[numsSize - 1] && target > nums[mid])
begin = mid + 1;
else
end = mid - 1;
}
}
return -1;
}边栏推荐
- 各位老板 问一下 就是我们mysql cdc保存的是配置数据 然后kafka里面堆积的有历史
- Discuss the important factors that affect the success or failure of automated testing
- BOM概述
- Kubernates-1.24.2 (latest version) + containerd + nexus
- Get all file names of the current folder
- [computer explanation] what should I pay attention to when I go to the computer repair shop to repair the computer?
- knapsack problem
- 【刷题笔记】搜索旋转排序数组
- 阿里云镜像地址&网易云镜像
- What are the types of financial products in 2022? Which is suitable for beginners?
猜你喜欢

数据提交类型 Request Payload 与 Form Data 的区别总结

论文阅读:UNET 3+: A FULL-SCALE CONNECTED UNET FOR MEDICAL IMAGE SEGMENTATION

如何在KVM环境中使用网络安装部署多台虚拟服务器

阿里云镜像地址&网易云镜像
![[daily question 1] 1184. Distance between bus stops](/img/36/2bbb8cc2a1fdd08070a5df7527e692.png)
[daily question 1] 1184. Distance between bus stops

Wechat applet switchtab transmit parameters and receive parameters
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (V)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (V)

Rambus announces ddr5 memory interface chip portfolio for data centers and PCs

Vscode saves setting configuration parameters to the difference between users and workspaces

YOLOv7模型推理和训练自己的数据集
随机推荐
Before Oracle 19C migration, how important is it to do a good job of rat playback test?
Purpose of SQL square brackets
Cointegraph wrote: relying on the largest Dao usdd to become the most reliable stable currency
New tea, start "fighting in groups"
Microorganisms are healthy. Don't exclude microorganisms in the human body
Ask the bosses: MySQL CDC stores configuration data, and Kafka has history
Talk about practice, do solid work, and become practical: tour the digitalized land of China
章鱼网络 Community Call #1|开启 Octopus DAO 构建
华为无线设备STA黑白名单配置命令
error: redefinition of
[OBS] DTS sent by video packet_ USEC calculation
【刷题笔记】搜索旋转排序数组
Wei Lai: what is the difference between multithreaded join and detach?
Decrypting numpy is a key difficulty in solving the gradient
微信小程序request请求携带cookie,验证是否已登录
[Yugong series] July 2022 go teaching course 016 logical operators and other operators of operators
Luo min's backwater battle in qudian
js无法获取headers中Content-Disposition
Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking
Tp5.1 foreach adds a new field in the controller record, and there is no need to write all the other fields again without changing them (not operating in the template) (paging)