当前位置:网站首页>【LeetCode】33、 搜索旋转排序数组
【LeetCode】33、 搜索旋转排序数组
2022-07-26 15:01:00 【小曲同学呀】
33、 搜索旋转排序数组
题目:
整数数组 nums 按升序排列,数组中的值 互不相同 。
在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (下标 从 0 开始 计数)。
例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。
给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 target ,则返回它的下标,否则返回 -1 。
你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。
示例1:
输入:nums = [4,5,6,7,0,1,2], target = 0
输出:4
示例2:
输入:nums = [4,5,6,7,0,1,2], target = 3
输出:-1
示例3:
输入:nums = [1], target = 0
输出:-1
提示:
1 <= nums.length <= 5000
-10^ 4 <= nums[i] <= 10^4
nums 中的每个值都 独一无二
题目数据保证 nums 在预先未知的某个下标上进行了旋转
-10^ 4 <= target <= 10^4
解题思路:
对于有序数组,可以使用二分查找的方法查找元素。
可以发现的是,我们将数组从中间分开成左右两部分的时候,一定有一部分的数组是有序的。拿示例来看,我们从 6 这个位置分开以后数组变成了 [4, 5, 6] 和 [7, 0, 1, 2] 两个部分,其中左边 [4, 5, 6] 这个部分的数组是有序的,其他也是如此。
这启示我们可以在常规二分查找的时候查看当前 mid 为分割位置分割出来的两个部分 [l, mid] 和 [mid + 1, r] 哪个部分是有序的,并根据有序的那个部分确定我们该如何改变二分查找的上下界,因为我们能够根据有序的那部分判断出 target 在不在这个部分:
如果 [l, mid - 1] 是有序数组,且 target 的大小满足[nums[l],nums[mid]),则我们应该将搜索范围缩小至 [l, mid - 1],否则在 [mid + 1, r] 中寻找。

需要注意的是,二分的写法有很多种,所以在判断 target 大小与有序部分的关系的时候可能会出现细节上的差别。
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
if (n == 0) {
return -1;
}
if (n == 1) {
return nums[0] == target ? 0 : -1;
}
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[n - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}

边栏推荐
- R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize the balloon graph (visualize the contingency table composed of two classification variables), and config
- How to get 5L water in a full 10L container, 7L or 4L empty container
- php反序列化部分学习
- 【静态代码质量分析工具】上海道宁为您带来SonarSource/SonarQube下载、试用、教程
- Remote desktop on Jetson nano
- jetson nano上远程桌面
- USB转串口参数配置功能
- Within a week, I developed my own knowledge sharing platform
- Leetcode summary
- How to find the translation of foreign literature for undergraduate graduation thesis?
猜你喜欢
随机推荐
Database expansion can also be so smooth, MySQL 100 billion level data production environment expansion practice
Deep packet inspection using quotient filter paper summary
Selenium code storage
Yifang biological fell 16% on the first day of listing: the company's market value was 8.8 billion, and Hillhouse and Lilly were shareholders
Soft test (VII) performance test (1) brief introduction
How to write the format of a university thesis?
NLP之NER:商品标题属性识别探索与实践
2023餐饮业展,中国餐饮供应链展,江西餐饮食材展2月举办
R language wilcox The test function compares whether there is a significant difference in the central position of the population of two nonparametric samples (if the two sample data are paired data, s
Which software must be used by scientific researchers to read literature?
USB转串口参数配置功能
OSPF和MGRE实验
The most detailed patent application tutorial, teaching you how to apply for a patent
How to find the translation of foreign literature for undergraduate graduation thesis?
Zhaoqi science and technology innovation high-end talent project was introduced and implemented, mass entrepreneurship and innovation competition was organized, and online live roadshow was broadcast
cs224w(图机器学习)2021冬季课程学习笔记5
最详细的专利申请教程,教你如何申请专利
FOC学习笔记-坐标变换以及仿真验证
Sharkteam releases Web3 security situational awareness report in the second quarter of 2022
R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize the balloon graph (visualize the contingency table composed of two classification variables), and config





![[basic] the difference between dynamic link library and static link library](/img/d5/fe7880e3fa91faff10a1c31870cce0.png)



