当前位置:网站首页>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];
}
}
边栏推荐
- Pcap learning notes II: pcap4j source code Notes
- Read PG in data warehouse in one article_ stat
- Mongodb command summary
- Conversion from non partitioned table to partitioned table and precautions
- ESP32系列专栏
- 【学习笔记】zkw 线段树
- [untitled]
- 10 张图打开 CPU 缓存一致性的大门
- 云检测2020:用于高分辨率遥感图像中云检测的自注意力生成对抗网络Self-Attentive Generative Adversarial Network for Cloud Detection
- [untitled]
猜你喜欢

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

人均瑞数系列,瑞数 4 代 JS 逆向分析

MATLAB中polarscatter函数使用

单片机学习笔记之点亮led 灯

Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion

基于鲲鹏原生安全,打造安全可信的计算平台

自定义线程池拒绝策略

日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则

Introduce six open source protocols in detail (instructions for programmers)

Blog recommendation | Apache pulsar cross regional replication scheme selection practice
随机推荐
API query interface for free mobile phone number ownership
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
DHCP 动态主机设置协议 分析
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
How to continue after handling chain interruption / sub chain error removed from scheduling
服务器到服务器 (S2S) 事件 (Adjust)
初学XML
将数学公式在el-table里面展示出来
error LNK2019: 无法解析的外部符号
Write it down once Net a new energy system thread surge analysis
ESP32构解工程添加组件
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
DrawerLayout禁止侧滑显示
学习突围2 - 关于高效学习的方法
Analysis of DHCP dynamic host setting protocol
Some principles of mongodb optimization
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
Mongodb meets spark (for integration)
Initialization script
【学习笔记】AGC010