当前位置:网站首页>【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;
}
}

边栏推荐
- php反序列化部分学习
- 下一代视觉Transformer:解锁CNN和Transformer正确结合方法
- R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses fitted function to calculate y value (response value) vector
- The R language uses the histogram function in the lattice package to visualize the histogram (histogram plot), the col parameter to customize the fill color, and the type parameter to customize the hi
- Simulation of character function and string function
- R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置add参数添加均值和标准差竖线、设置error.plot参数实际显示箱体
- Within a week, I developed my own knowledge sharing platform
- [leave some code] Apply transformer to target detection, and understand the model operation process of the model through debug
- C # set different text watermarks for each page of word
- 楚环科技深交所上市:市值27亿 民生证券是股东
猜你喜欢

How to write the format of a university thesis?

OpenGL学习日记2——着色器

How to query foreign literature?

1. Sum of two numbers

Chapter 08_ Principles of index creation and design

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

Cs224w (Figure machine learning) 2021 winter course learning notes 5

OSPF和MGRE实验

Deep Packet Inspection Using Cuckoo Filter论文总结

【LeetCode每日一题】——268.丢失的数字
随机推荐
How to query foreign literature?
Practical purchasing skills, purchasing methods of five bottleneck materials
Solve the problem that typora pictures cannot be displayed
R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(设置min.segment.length参数为0为每个数据点的标签添加线段)
OSPF和MGRE实验
JMeter distributed
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
Li Hongyi, machine learning 3. Gradient descent
Parallel d-Pipeline: A Cuckoo Hashing Implementation for Increased Throughput论文总结
Environment regulation system based on Internet of things (esp32-c3+onenet+ wechat applet)
What are the skills and methods of searching foreign literature
持续集成(一)基本概念简要介绍
Operation method of abbkine elikine human alpha fetoprotein (AFP) ELISA quantitative Kit
JS to realize the number to amount price thousand separator
R语言ggplot2可视化:使用ggplot2可视化散点图、使用ggpubr包的theme_pubclean函数设置可视化图像不包含坐标轴线的主题(theme without axis lines)
About the selection of industrial control gateway IOT serial port to WiFi module and serial port to network port module
R language uses LM function to build a multiple regression model with interactive terms, and uses step function to build a stepwise regression model to screen the best subset of predictive variables (
sqlDeveloper工具快速入门
Lean product development: principles, methods and Implementation
不到一周我开发出了属于自己的知识共享平台