当前位置:网站首页>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)
边栏推荐
- A4988 and 42 stepper motors
- 鸿蒙第四次学习
- 【愚公系列】2022年07月 Go教学课程 001-Go语言前提简介
- PR曲线和ROC曲线概念及其区别
- “栈”的典型应用—表达式求值(C语言实现)
- 【愚公系列】2022年07月 Go教学课程 001-Go语言前提简介
- exness深度好文:动性系列-黄金流动性实例分析(五)
- Interview, about thread pool
- Another double non reform exam 408, will it be cold? Software College of Nanchang Aviation University
- Architecture design - ID generator "suggestions collection"
猜你喜欢
UE4 draw a circle with spline
Summary of fun free GM games
Night God simulator +fiddler packet capture test app
Wechat applet video sharing platform system graduation design completion (5) assignment
What is cloud primordial? This time, I can finally understand!
Uncover the whole link communication process of dewu customer service im
Ue4 dessine un cercle avec une ligne de contour
工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
什么是云原生?这回终于能搞明白了!
随机推荐
距离度量 —— 杰卡德距离(Jaccard Distance)
How to set vscode to delete the whole line shortcut key?
ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null
Architecture design - ID generator "suggestions collection"
Responses of different people in technology companies to bugs | daily anecdotes
链游系统开发(Unity3D链游开发详情)丨链游开发成熟技术源码
Troubleshooting ideas that can solve 80% of faults
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
Esp32-c3 introductory tutorial question ⑩ - error: implicit declaration of function 'ESP_ blufi_ close‘;
UE4 用spline畫正圓
27:第三章:开发通行证服务:10:【注册/登录】接口:注册/登录OK后,把用户会话信息(uid,utoken)保存到redis和cookie中;(一个主要的点:设置cookie)
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
Uncover the whole link communication process of dewu customer service im
Eliminate the yellow alarm light on IBM p750 small computer [easy to understand]
options should NOT have additional properties
1.5.1版本官方docker镜像运行容器,能设置使用 mysql 8驱动吗?
Wechat applet video sharing platform system graduation design (3) background function
消除IBM P750小机上的黄色报警灯[通俗易懂]
Memory mapping of QT
options should NOT have additional properties