当前位置:网站首页>Leetcode(81)——搜索旋转排序数组 II
Leetcode(81)——搜索旋转排序数组 II
2022-07-02 17:02:00 【SmileGuy17】
Leetcode(81)——搜索旋转排序数组 II
题目
已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。
在传递给函数之前,nums 在预先未知的某个下标 k( 0 < = k < n u m s . l e n g t h 0 <= k < nums.length 0<=k<nums.length)上进行了 旋转 ,使数组变为 [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 [4,5,6,6,7,0,1,2,4,4] 。
给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 nums 中存在这个目标值 target ,则返回 true ,否则返回 false 。
你必须尽可能减少整个操作步骤。
示例 1:
输入:nums = [2,5,6,0,0,1,2], target = 0
输出:true
示例 2:
输入:nums = [2,5,6,0,0,1,2], target = 3
输出:false
提示:
1 1 1 <= nums.length <= 5000 5000 5000
− 1 0 4 -10^4 −104 <= nums[i] <= 1 0 4 10^4 104
题目数据保证 n u m s nums nums 在预先未知的某个下标上进行了旋转
− 1 0 4 -10^4 −104 <= target <= 1 0 4 10^4 104
进阶:
- 这是 搜索旋转排序数组 的延伸题目,本题中的 n u m s nums nums 可能包含重复元素。
- 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
题解
方法一:二分查找
思路
但和 33. 搜索旋转排序数组 不同的是,本题元素并不唯一。这意味着我们无法直接根据与 nums[0]nums[0] 的大小关系,将数组划分为两段,即无法通过「二分」来找到旋转点。因为「二分」的本质是二段性,并非单调性。只要一段满足某个性质,另外一段不满足某个性质,就可以用「二分」。
即使数组被旋转过,我们仍然可以利用这个数组的递增性,使用二分查找。对于当前的中点,如果它指向的值小于等于右端,那么说明右区间是排好序的;反之,那么说明左区间是排好序的。如果目标值位于排好序的区间内,我们可以对这个区间继续二分查找;反之,我们对于另一半区间继续二分查找。
注意,因为数组存在重复数字,如果中点和左端的数字相同,我们并不能确定是左区间全部相同,还是右区间完全相同。在这种情况下,我们可以简单地将左端点右移一位,然后继续进行二分查找。
例如 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,首次二分时无法判断区间 [ 0 , 3 ] [0,3] [0,3] 和区间 [ 4 , 6 ] [4,6] [4,6] 哪个是有序的。
代码实现
Leetcode 官方题解:
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]) {
// 去掉首尾重复的元素,保证二段性
++l;
--r;
} else if (nums[l] <= nums[mid]) {
// 左区间是排好序的了
if (nums[l] <= target && target < nums[mid]) // 判断 target 是否在左区间里
r = mid - 1;
else l = mid + 1;
} else {
// 右区间是排好序的了
if (nums[mid] < target && target <= nums[n - 1]) // 判断 target 是否在右区间里
l = mid + 1;
else r = mid - 1;
}
}
return false;
}
};
复杂度分析
时间复杂度: O ( log n ) O(\log{n}) O(logn)。最坏情况下数组元素均相等且不为 target \textit{target} target,我们需要访问所有位置才能得出结果。此时的时间复杂度为 O ( n ) O(n) O(n),其中 n n n 是数组 nums \textit{nums} nums 的长度。
空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
- Pit encountered during installation of laravel frame
- Unity学习shader笔记[八十一]简单的颜色调整后处理(亮度,饱和度,对比度)
- 300+ documents! This article explains the latest progress of multimodal learning based on transformer
- NM01-独立于总线协议的NM模块功能概述与API定义
- C# 检测图片是否被旋转并修改到正真的旋转
- A4988 and 42 stepper motors
- RDK simulation experiment
- 再放宽!这些应届生,可直接落户上海
- MySQL about only_ full_ group_ By limit
- Installation tutorial and simple call of Matplotlib
猜你喜欢
Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline
300+ documents! This article explains the latest progress of multimodal learning based on transformer
Editor Editor Extension add button and logo in scene view
微信核酸检测预约小程序系统毕业设计毕设(5)任务书
Simulateur nightGod + application de test de capture de paquets Fiddler
再放寬!這些應届生,可直接落戶上海
夜神模擬器+Fiddler抓包測試App
Wechat applet video sharing platform system graduation design completion (6) opening defense ppt
SteamOS 3.3 Beta 发布,Steam Deck 中文键盘终于来了
微信小程序视频分享平台系统毕业设计毕设(1)开发概要
随机推荐
RTE11- 中断解耦功能
Babbitt | metauniverse daily must read: can you buy a virtual anchor for 1000 yuan? Is this the live gospel of small businesses or "cutting leeks"
Esp32-c3 introductory tutorial question ⑪ - ESP tls: create_ ssl_ handle failed, tls_ io_ instance->options. trusted_ certs null
初夏,开源魔改一个带击杀音效的电蚊拍!
Export Excel files using npoi
Unity learning shader notes [81] simple color adjustment post-processing (brightness, saturation, contrast)
Esp32-c3 introductory tutorial question ⑩ - error: implicit declaration of function 'ESP_ blufi_ close‘;
Another double non reform exam 408, will it be cold? Software College of Nanchang Aviation University
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
服务器php环境搭建教程,PHP服务端环境搭建图文详解
呆错图床系统源码图片CDN加速与破J防盗链功能
Wechat applet video sharing platform system graduation design completion (5) assignment
Remember to use ternary expressions when switching transformations
Detailed explanation of map set
QQmlApplicationEngine
2020互联网行业术语
[Oracle final review] addition, deletion and modification of tablespaces, tables, constraints, indexes and views
再放宽!这些应届生,可直接落户上海
2020 Internet industry terminology
Paddlepaddle 28 build an automatic coder based on convolution