当前位置:网站首页>LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
2022-07-07 11:27:00 【小城老街】
1.题目
已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转后,得到输入数组。例如,原数组 nums = [0,1,2,4,5,6,7] 在变化后可能得到:
若旋转 4 次,则可以得到 [4,5,6,7,0,1,2]
若旋转 7 次,则可以得到 [0,1,2,4,5,6,7]
注意,数组 [a[0], a[1], a[2], …, a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], …, a[n-2]] 。
给你一个元素值互不相同的数组 nums ,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的最小元素 。
你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。
示例 1:
输入:nums = [3,4,5,1,2]
输出:1
解释:原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。
示例 2:
输入:nums = [4,5,6,7,0,1,2]
输出:0
解释:原数组为 [0,1,2,4,5,6,7] ,旋转 4 次得到输入数组。
示例 3:
输入:nums = [11,13,15,17]
输出:11
解释:原数组为 [11,13,15,17] ,旋转 4 次得到输入数组。
提示:
n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
nums 中的所有整数互不相同
nums 原来是一个升序排序的数组,并进行了 1 至 n 次旋转
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array
2.思路
(1)二分搜索
思路参考本题官方题解。
3.代码实现(Java)
//思路1————二分搜索
class Solution {
public static int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
//如果 nums[left] < nums[right],则说明 nums[left...right] 之间的元素是升序的,所以最小元素就是 nums[left]
if (nums[left] < nums[right]) {
return nums[left];
}
int mid = left + (right - left) / 2;
if (nums[mid] < nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[right];
}
}
边栏推荐
- LED light of single chip microcomputer learning notes
- Simple and easy-to-use code specification
- 聊聊伪共享
- regular expression
- MongoDB复制(副本集)总结
- OSI 七层模型
- 云检测2020:用于高分辨率遥感图像中云检测的自注意力生成对抗网络Self-Attentive Generative Adversarial Network for Cloud Detection
- ESP32构解工程添加组件
- 【Presto Profile系列】Timeline使用
- 飞桨EasyDL实操范例:工业零件划痕自动识别
猜你喜欢

leecode3. 无重复字符的最长子串

靠卖概念上市,认养一头牛能走多远?

Milkdown 控件图标

“新红旗杯”桌面应用创意大赛2022
![[learning notes] zkw segment tree](/img/18/21f455a06e8629243fc5cf4df0044c.png)
[learning notes] zkw segment tree

Write it down once Net a new energy system thread surge analysis
![Scripy tutorial classic practice [New Concept English]](/img/bc/f1ef8b6de6bfb6afcdfb0d45541c72.png)
Scripy tutorial classic practice [New Concept English]

【Presto Profile系列】Timeline使用

Smart cloud health listed: with a market value of HK $15billion, SIG Jingwei and Jingxin fund are shareholders

Sed of three swordsmen in text processing
随机推荐
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
MongoDB内部的存储原理
Go语言学习笔记-结构体(Struct)
How to make the new window opened by electorn on the window taskbar
JS判断一个对象是否为空
JNA学习笔记一:概念
Write it down once Net a new energy system thread surge analysis
[untitled]
Practical example of propeller easydl: automatic scratch recognition of industrial parts
[untitled]
Summary of import, export, backup and recovery of mongodb
ORACLE进阶(五)SCHEMA解惑
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
将数学公式在el-table里面展示出来
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
国泰君安证券开户怎么开的?开户安全吗?
Cinnamon Applet 入门
MySQL入门尝鲜
ClickHouse(03)ClickHouse怎么安装和部署