当前位置:网站首页>LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
2022-08-02 01:55:00 【light [email protected]】
已知一个长度为 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 次旋转
思路:与搜索旋转排序数组差别不大
题解:
class Solution {
public int findMin(int[] nums) {
int res = Integer.MAX_VALUE;
int start = 0, end = nums.length-1;
if(nums.length == 0){
return -1;
}
while (start <= end) {
int mid = (start + end)/2;
// 查找最小值
res = Math.min(res, nums[start]);
res = Math.min(res, nums[end]);
res = Math.min(res, nums[mid]);
if(nums[start] <= nums[mid]){
if(nums[start] <= res && res < nums[mid] ){
end = mid-1;
}else{
start = mid + 1;
}
}else{
if(nums[mid] < res && res <= nums[nums.length-1]){
start = mid + 1;
}else{
end = mid - 1;
}
}
}
return res;
}
}
版权声明
本文为[light [email protected]~no trace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020144011216.html
边栏推荐
- TKU记一次单点QPS优化(顺祝ITEYE终于回来了)
- Constructor of typescript35-class
- JDBC PreparedStatement 的命名参数实现
- typescript29-枚举类型的特点和原理
- When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
- 牛顿定理和相关推论
- Navicat data shows incomplete resolution
- LeetCode刷题日记: 33、搜索旋转排序数组
- typescript36-class的构造函数实例方法
- Day116. Shangyitong: Details of appointment registration ※
猜你喜欢
Navicat data shows incomplete resolution
Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program
6-25漏洞利用-irc后门利用
【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)
Image fusion based on weighted 】 and pyramid image fusion with matlab code
Constructor instance method of typescript36-class
飞桨助力航天宏图PIE-Engine地球科学引擎构建
6-24漏洞利用-vnc密码破解
【图像融合】基于加权和金字塔实现图像融合附matlab代码
电子制造仓储条码管理系统解决方案
随机推荐
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
【图像融合】基于加权和金字塔实现图像融合附matlab代码
力扣、752-打开转盘锁
When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
求大神解答,这种 sql 应该怎么写?
6-25 Vulnerability Exploitation - irc Backdoor Exploitation
MySQL——增删查改操作
【ORB_SLAM2】SetPose、UpdatePoseMatrices
外包干了三年,废了...
秒懂大模型 | 3步搞定AI写摘要
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
bool Frame::PosInGrid(const cv::KeyPoint &kp, int &posX, int &posY)
typescript38-class的构造函数实例方法继承(implement)
typescript30-any类型
搜罗汇总的效应
待读书单列表
volatile原理解析
滴滴秋招提前批正式开始,现在投递免笔试
Can't connect to MySQL server on 'localhost3306' (10061) Simple and clear solution