当前位置:网站首页>[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;
}边栏推荐
- Oracle table creation statement template
- 第一启富金怎么样
- Default value of dart variable
- Two week learning results of machine learning
- 章鱼网络 Community Call #1|开启 Octopus DAO 构建
- Will eating fermented steamed bread hurt your body
- 批量导入数据,一直提示 “失败原因:SQL解析失败:解析文件失败::null”怎么回事?
- Purpose of SQL square brackets
- 【terminal】x86 Native Tools Command Prompt for VS 2017
- When importing data in batches, you always prompt "failure reason: SQL parsing failure: parsing file failure:: null". What's the matter?
猜你喜欢

leetcode刷题:动态规划06(整数拆分)

Statistical learning -- naive Bayesian method

PADS导出gerber文件
![[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x](/img/7e/1d27e3f1856ab8c6cbfc5221c717bb.png)
[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x

js无法获取headers中Content-Disposition

Scavenging vultures or woodpeckers? How to correctly understand short selling

Software engineering in Code: regular expression ten step clearance

Not only log collection, but also the installation, configuration and use of project monitoring tool sentry

QT6 with vs Code: compiling source code and basic configuration

Devops has been practiced for many years. What is the most painful thing?
随机推荐
BOM概述
Price reduction, game, bitterness, etc., vc/pe rushed to queue up and quit in 2022
【电脑讲解】NVIDIA发布GeForce RTX SUPER系列显卡,游戏玩家福利来了!
30 times performance improvement -- implementation of MyTT index library based on dolphin DB
【每日一题】剑指 Offer II 115. 重建序列
【SemiDrive源码分析】【驱动BringUp】39 - Touch Panel 触摸屏调试
150. Evaluation of inverse Polish expression
一日千里,追风逐月 | 深势科技发布极致加速版分子对接引擎Uni-Docking
Electronic Association C language level 2 60, integer parity sort (real question in June 2021)
10 minutes to understand how JMeter plays with redis database
【每日一题】1184. 公交站间的距离
js无法获取headers中Content-Disposition
paddlepaddle 34 调整模型的layer结构与forward流程(实现layer的增删改与forward的修改)
Two week learning results of machine learning
各位老板 问一下 就是我们mysql cdc保存的是配置数据 然后kafka里面堆积的有历史
Luo min's backwater battle in qudian
北京内推 | 微软STCA招聘NLP/IR/DL方向研究型实习生(可远程)
Purpose of SQL square brackets
Hierarchical reinforcement learning: a comprehensive survey
Ideal L9, can't cross a pit on the road?