当前位置:网站首页>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
边栏推荐
- Analysis of volatile principle
- Ask God to answer, how should this kind of sql be written?
- Newton's theorem and related corollaries
- typescript31-any类型
- Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
- 滴滴秋招提前批正式开始,现在投递免笔试
- 制造企业数字化转型现状分析
- Use baidu EasyDL implement factory workers smoking behavior recognition
- 『网易实习』周记(三)
- typescript32-ts中的typeof
猜你喜欢

浅谈国产ERP的“横纵竖”三向发展态势

Constructor instance method inheritance of typescript38-class (implement)

6-25 Vulnerability Exploitation - irc Backdoor Exploitation

typeof in typescript32-ts

Navicat数据显示不完全的解决方法

Understand the big model in seconds | 3 steps to get AI to write a summary

传统企业数字化转型需要经过几个阶段?

Hash collisions and consistent hashing

Two ways to pass feign exceptions: fallbackfactory and global processing Get server-side custom exceptions

Can't connect to MySQL server on 'localhost3306' (10061) Simple and clear solution
随机推荐
typescript32-ts中的typeof
编码经验之谈
MySQL optimization strategy
三本毕业的我被腾讯拒绝了十四次,最终成功入职阿里
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
typescript35-class的构造函数
volatile原理解析
Shell入门终章
"Introduction to Natural Language Processing Practice" Question Answering Robot Based on Knowledge Graph
飞桨助力航天宏图PIE-Engine地球科学引擎构建
电商库存系统的防超卖和高并发扣减方案
LeetCode Brushing Diary: 74. Searching 2D Matrix
信息化和数字化的本质区别是什么?
Typescript31 - any type
Chengdu openGauss user group recruit!
第一次写对牛客的编程面试题:输入一个字符串,返回该字符串出现最多的字母
Can't connect to MySQL server on 'localhost3306' (10061) Simple and clear solution
typescript33-typescript高级概述
YGG Guild Development Plan Season 1 Summary
HSDC is related to Independent Spanning Tree