当前位置:网站首页>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)
边栏推荐
- Responses of different people in technology companies to bugs | daily anecdotes
- Matlab中弧度转角度、角度转弧度
- Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用
- [Northwestern Polytechnic University] information sharing of the first and second postgraduate examinations
- 初夏,开源魔改一个带击杀音效的电蚊拍!
- Night God simulator +fiddler packet capture test app
- Use dosbox to run the assembly super detailed step "suggestions collection"
- 故障排查:kubectl报错ValidationError: unknown field \u00a0
- 呆错图床系统源码图片CDN加速与破J防盗链功能
- AI开发调试系列第二弹:多机分布式调测探索之旅
猜你喜欢

Qt Official examples: Qt Quick Controls - Gallery
![Unity learning shader notes [82] black and white processing of enhanced single channel color rendering](/img/db/d745a434e76511742d1264706b5d9a.png)
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering

Leetcode 面试题 17.04. 消失的数字

RDK仿真实验

科技公司不同人对Bug的反应 | 每日趣闻

Night God simulator +fiddler packet capture test app

Leetcode interview question 17.01 Addition without plus sign

Redis (6) -- object and data structure

Implementation shadow introduction

Please, stop painting star! This has nothing to do with patriotism!
随机推荐
@Component cannot get Dao layer
再放宽!这些应届生,可直接落户上海
夜神模拟器+Fiddler抓包测试App
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
Typical application of "stack" - expression evaluation (implemented in C language)
阿里三面被面试官狂问Redis,简历上再也不敢写'精通'了
怎么用ps提取图片颜色分析色彩搭配
实施阴影介绍
Leetcode interview question 16.17 Continuous sequence
Qt官方示例:Qt Quick Controls - Gallery
Leetcode 面试题 16.11. 跳水板
Leetcode 面试题 16.15. 珠玑妙算
Three methods of MySQL backup
iptable端口重定向 MASQUERADE[通俗易懂]
Architecture design - ID generator "suggestions collection"
元宇宙链游系统开发(逻辑开发)丨链游系统开发(详细分析)
Leetcode 面试题 16.17. 连续数列
RDK simulation experiment
300+篇文献!一文详解基于Transformer的多模态学习最新进展