当前位置:网站首页>Leetcode (81) -- search rotation sort array II
Leetcode (81) -- search rotation sort array II
2022-07-02 18:43:00 【SmileGuy17】
Leetcode(81)—— Search rotation sort array II
subject
It is known that there is an array of integers in non descending order nums , The values in the array don't have to be different from each other .
Before passing it to a function ,nums In some unknown subscript k( 0 < = k < n u m s . l e n g t h 0 <= k < nums.length 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,4,4,5,6,6,7] In subscript 5 It may turn into [4,5,6,6,7,0,1,2,4,4] .
Here you are. After rotation Array of nums And an integer target , Please write a function to determine whether the given target value exists in the array . If nums There is a target value in target , Then return to true , Otherwise return to false .
You must minimize the whole procedure .
Example 1:
Input :nums = [2,5,6,0,0,1,2], target = 0
Output :true
Example 2:
Input :nums = [2,5,6,0,0,1,2], target = 3
Output :false
Tips :
1 1 1 <= nums.length <= 5000 5000 5000
− 1 0 4 -10^4 −104 <= nums[i] <= 1 0 4 10^4 104
Topic data assurance n u m s nums nums Rotated on a previously unknown subscript
− 1 0 4 -10^4 −104 <= target <= 1 0 4 10^4 104
Advanced :
- This is a Search rotation sort array The extended topic of , In this question n u m s nums nums It may contain repeating elements .
- Does this affect the time complexity of the program ? What kind of impact will it have , Why? ?
Answer key
Method 1 : Two points search
Ideas
But and 33. Search rotation sort array The difference is , The element of this question is not unique . This means that we cannot directly base on nums[0]nums[0] The size of the relationship , Divide the array into two segments , I.e. unable to pass 「 Two points 」 To find the rotation point . because 「 Two points 」 The essence of is dualism , 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 」.
Even if the array is rotated , We can still take advantage of the incremental nature of this array , Use binary search . For the current midpoint , If it points to a value less than or equal to the right end , Then it shows that the right interval is well ordered ; conversely , Then it shows that the left interval is in good order . If the target value is within the ordered interval , We can continue the binary search for this interval ; conversely , Let's continue the binary search for the other half of the interval .
Be careful , Because there are duplicate numbers in the array , If the middle and left numbers are the same , We can't be sure that the left intervals are all the same , Or is the right interval exactly the same . under these circumstances , We can simply move the left endpoint one bit to the right , Then continue the binary search .
for example nums = [ 3 , 1 , 2 , 3 , 3 , 3 , 3 ] \textit{nums}=[3,1,2,3,3,3,3] nums=[3,1,2,3,3,3,3], target = 2 \textit{target}=2 target=2, The interval cannot be judged at the first dichotomy [ 0 , 3 ] [0,3] [0,3] And interval [ 4 , 6 ] [4,6] [4,6] Which is orderly .
Code implementation
Leetcode Official explanation :
class Solution {
public:
bool search(vector<int> &nums, int target) {
int n = nums.size();
if (n == 0) return false;
else if (n == 1) return nums[0] == target;
int l = 0, r = n - 1, mid;
while (l <= r) {
mid = (l + r) / 2;
if (nums[mid] == target) return true;
if (nums[l] == nums[mid] && nums[mid] == nums[r]) {
// Remove the repeating elements , Ensure the duality
++l;
--r;
} else if (nums[l] <= nums[mid]) {
// The left interval is in order
if (nums[l] <= target && target < nums[mid]) // Judge target Is it in the left section
r = mid - 1;
else l = mid + 1;
} else {
// The right interval is in order
if (nums[mid] < target && target <= nums[n - 1]) // Judge target Is it in the right section
l = mid + 1;
else r = mid - 1;
}
}
return false;
}
};
Complexity analysis
Time complexity : O ( log n ) O(\log{n}) O(logn). In the worst case, the array elements are equal and not target \textit{target} target, We need to visit all locations to get results . The time complexity is O ( n ) O(n) O(n), among n n n It's an array nums \textit{nums} nums The length of .
Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- StretchDIBits函数
- 任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
- 2020 Internet industry terminology
- Unity learning shader notes [81] simple color adjustment post-processing (brightness, saturation, contrast)
- 27:第三章:开发通行证服务:10:【注册/登录】接口:注册/登录OK后,把用户会话信息(uid,utoken)保存到redis和cookie中;(一个主要的点:设置cookie)
- Radian to angle, angle to radian in MATLAB
- Simulateur nightGod + application de test de capture de paquets Fiddler
- Leetcode(81)——搜索旋转排序数组 II
- 工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
- options should NOT have additional properties
猜你喜欢

QT official example: QT quick controls - Gallery

工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座

初夏,开源魔改一个带击杀音效的电蚊拍!

Yesterday, Alibaba senior wrote a responsibility chain model, and there were countless bugs

Pit encountered during installation of laravel frame

UE4 用spline畫正圓

夜神模擬器+Fiddler抓包測試App

Leetcode interview question 16.15 Abacus wonderful calculation

Qt Official examples: Qt Quick Controls - Gallery

一款简约PHP个人发卡程序V4.0版本
随机推荐
Ali was wildly asked by the interviewer on three sides. Redis dared not write 'proficient' on his resume anymore
The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown
Memory mapping of QT
【Oracle 期末复习】表空间、表、约束、索引、视图的增删改
UE4 draw a circle with spline
再放宽!这些应届生,可直接落户上海
Concepts and differences of PR curve and ROC curve
SAP S/4HANA OData Mock Service 介绍
How to use PS to extract image color and analyze color matching
“栈”的典型应用—表达式求值(C语言实现)
paddlepaddle 28 搭建基于卷积的自动编码机
Leetcode(81)——搜索旋转排序数组 II
Use dosbox to run the assembly super detailed step "suggestions collection"
Matlab中弧度转角度、角度转弧度
昨天阿里学长写了一个责任链模式,竟然出现了无数个bug
Simulateur nightGod + application de test de capture de paquets Fiddler
options should NOT have additional properties
IPtable port redirection masquerade[easy to understand]
深度神经网络总结
Steamos 3.3 beta release, steam deck Chinese keyboard finally came