当前位置:网站首页>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)
边栏推荐
- SLAM|如何时间戳对齐?
- 怎么用ps提取图片颜色分析色彩搭配
- Aptos tutorial - participate in the official incentive testing network (ait2 incentive testing network)
- 昨天阿里学长写了一个责任链模式,竟然出现了无数个bug
- Wechat applet video sharing platform system graduation design completion (4) opening report
- MySQL 关于 only_full_group_by 限制
- Leetcode interview question 17.04 Vanishing numbers
- 服务器php环境搭建教程,PHP服务端环境搭建图文详解
- ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null
- Three ways of function parameter transfer in C language
猜你喜欢
Leetcode(81)——搜索旋转排序数组 II
Wechat applet video sharing platform system graduation design completion (8) graduation design thesis template
Leetcode interview question 17.01 Addition without plus sign
Relax again! These fresh students can settle directly in Shanghai
Simulateur nightGod + application de test de capture de paquets Fiddler
距离度量 —— 杰卡德距离(Jaccard Distance)
Wechat applet video sharing platform system graduation design completion (7) Interim inspection report
NM01-独立于总线协议的NM模块功能概述与API定义
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
Ali was wildly asked by the interviewer on three sides. Redis dared not write 'proficient' on his resume anymore
随机推荐
饭卡 HDU2546
“栈”的典型应用—表达式求值(C语言实现)
Redis (7) -- database and expiration key
科技公司不同人对Bug的反应 | 每日趣闻
A good programmer is worth five ordinary programmers!
Radian to angle, angle to radian in MATLAB
Use dosbox to run the assembly super detailed step "suggestions collection"
工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
Is Guojin securities a state-owned enterprise? Is it safe to open an account in Guojin securities?
又一所双非改考408,会爆冷么?南昌航空大学软件学院
Relax again! These fresh students can settle directly in Shanghai
ESP32-C3入门教程 问题篇⑩——error: implicit declaration of function ‘esp_blufi_close‘;
Installation tutorial and simple call of Matplotlib
Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用
Tower safety monitoring system unattended inclination vibration monitoring system
呆错图床系统源码图片CDN加速与破J防盗链功能
Wechat applet video sharing platform system graduation design (2) applet function
Chain game system development (unity3d chain game development details) - chain game development mature technology source code
Server PHP environment building tutorial, PHP server environment building graphic explanation
paddlepaddle 28 搭建基于卷积的自动编码机