当前位置:网站首页>【每日一题】【leetcode】25. 数组-旋转数组的最小数字
【每日一题】【leetcode】25. 数组-旋转数组的最小数字
2022-08-10 15:04:00 【aneutron】
题目
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一个旋转,该数组的最小值为1。 难易程度:easy
示例 1:
输入:[3,4,5,1,2] 输出:1
示例 2:
输入:[2,2,2,0,1] 输出:0
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
方法一
分析
首先,题目中给出的数组是部分有序。最小的元素就是旋转点:
- 前后元素都比他大
- 前边的子数组是递增的
- 后边的子数组是递增的
因此,直观的解法就是遍历,找到第一个位置:numbers[l] < numbers[r]
。 时间复杂度:O(N) 空间复杂度:O(1)
代码
class Solution {
public:
int minArray(vector<int>& numbers) {
int l = 0, r = numbers.size() - 1;
while (l < r) {
if (numbers[l] >= numbers[r]) {
l++;
} else {
break;
}
}
return numbers[l];
}
};
方法二
分析
题意已经明确,原始数组是由有序数组旋转生成,设最小值的索引为mid
,则[0, mid)
和[mid, size)
是两个有序数组。查找最小值本身就是搜索问题。本题题意抽取出如下两点:
- 有序
- 查找
因此,可以考虑二分搜索算法,判断条件如下;
numbers[mid] > numbers
, 则l = mid + 1
numbers[mid] < numbers
, 则r = mid
numbers[mid] == numbers
,特殊的点就在这儿,此时,最小值只能在[l, r)
,因此r--
时间复杂度:O(logN) 空间复杂度:O(1)
代码
class Solution {
public:
int minArray(vector<int>& numbers) {
int l = 0, r = numbers.size() - 1;
while (l < r) {
int mid = ((r - l) >> 1) + l;
if (numbers[mid] > numbers[r]) {
l = mid + 1;
} else if (numbers[mid] < numbers[r]) {
r = mid;
} else {
r--;
}
}
return numbers[r];
}
};
边栏推荐
- 富爸爸穷爸爸之读书笔记
- pm2之静态文件服务
- It is reported that the original Meitu executive joined Weilai mobile phone, the top product may exceed 7,000 yuan
- Problem solving-->Online OJ (19)
- Websocket realizes real-time change of chart content
- 【吴恩达来信】强化学习的发展!
- Oracle数据库备份dmp文件太大,有什么办法可以在备份的时候拆分成多个dmp吗?
- Oracle database backup DMP file is too big, what method can be split into multiple DMP when backup?
- Systemui status bar to add a new icon
- “低代码”编程或将是软件开发的未来
猜你喜欢
MySQL Principle and Optimization: Update Optimization
fastposter v2.9.1 程序员必备海报生成器
Appium进行APP自动化测试
一个 ABAP Development Tool 自定义 service endpoint 的测试工具
FP6378AS5CTR SOT-23-5 高效1MHz2A同步降压调节器
Oracle数据库备份dmp文件太大,有什么办法可以在备份的时候拆分成多个dmp吗?
A Sina Weibo semantic sentiment analysis tool developed by ABAP
Zijin Example
Parse the value of uuid using ABAP regular expressions
Oracle database backup DMP file is too big, what method can be split into multiple DMP when backup?
随机推荐
[Letter from Wu Enda] The development of reinforcement learning!
解题-->在线OJ(十九)
fastposter v2.9.1 programmer must-have poster generator
Lilac Garden
SWIG Tutorial "One"
NFT digital collection development issue - digital collection platform
常见SQL、API接口等常见约定
智为链接,慧享生活,荣耀智慧服务,只为 “懂” 你
Detailed understanding of all built-in functions (Part 2)
Software Test Cases
“蔚来杯“2022牛客暑期多校训练营7
JS 从零手写实现一个bind方法
HUAWEI CLOUD DevCloud received the highest-level certification of the first batch of cloud-native technology architecture maturity assessments by the China Academy of Information and Communications Te
$'\r': command not found
Oracle database backup DMP file is too big, what method can be split into multiple DMP when backup?
const修饰的指针变量(详解)
systemui状态栏添加新图标
消息称原美图高管加盟蔚来手机 顶配产品或超7000元
产品说明丨如何使用MobPush快速创建应用
【芯片】人人皆可免费造芯?谷歌开源芯片计划已释放90nm、130nm和180nm工艺设计套件