当前位置:网站首页>【leetcode】153. Find the lowest value in the rotation sort array
【leetcode】153. Find the lowest value in the rotation sort array
2022-06-29 00:26:00 【Chinese fir sauce_】
subject :
153. Look for the minimum value in the rotation sort array
We know a length of n Array of , In ascending order in advance , Through 1 To n Time rotate after , Get the input array . for example , Original array nums = [0,1,2,4,5,6,7] After the change may get :
If you rotate 4 Time , You can get [4,5,6,7,0,1,2]
If you rotate 7 Time , You can get [0,1,2,4,5,6,7]
Be careful , Array [a[0], a[1], a[2], …, a[n-1]] Rotate once The result is an array [a[n-1], a[0], a[1], a[2], …, a[n-2]] .
Give you an element value Different from each other Array of nums , It turns out to be an ascending array , According to the above situation, we have made many rotations . Please find and return the The smallest element .
Example 1:
Input :nums = [3,4,5,1,2]
Output :1
explain : The original array is [1,2,3,4,5] , rotate 3 Get the input array for the first time .
Example 2:
Input :nums = [4,5,6,7,0,1,2]
Output :0
explain : The original array is [0,1,2,4,5,6,7] , rotate 4 Get the input array for the first time .
Example 3:
Input :nums = [11,13,15,17]
Output :11
explain : The original array is [11,13,15,17] , rotate 4 Get the input array for the first time .
Dichotomy :
Because the array does not contain duplicate elements , And as long as the current interval length is not 1, mid It won't be with high coincidence ; If the current interval length is 1, This shows that we can end the two-point search .
When the binary search ends (l=r), We get the position of the minimum .
class Solution {
public int findMin(int[] nums) {
int n = nums.length;
int l = 0,r = n - 1;
while(l < r){
int mid = (l + r) / 2;
// Order on the right
if(nums[r] >= nums[mid]) r = mid;
// Order on the left
else if(nums[r] < nums[mid]) l = mid + 1;
}
return nums[l];
}
}
边栏推荐
- Technology sharing | software development process that you must understand if you want to get started with testing
- Typescript -- Section 5: classes
- 每日一题: 数组中数字出现的次数
- Mapbox GL loading local publishing DEM data
- ES6 module
- scp拷贝文件夹
- Leetcode daily question: implementing strstr()
- Precautions for installation and use of rotary joint
- Daily question 1: the number of numbers in the array 2
- Along with the notes: methods simulating array like classes
猜你喜欢
![[buuctf.reverse] 131-135](/img/c2/b8b06c8191af2c75bf4ad5c82feaea.png)
[buuctf.reverse] 131-135

Structure of the actual combat battalion | module 5

be based on. NETCORE development blog project starblog - (13) add friendship link function

Report on the convenient bee Lantern Festival: the first peak sales of pasta products this year; prefabricated wine dumplings became the winners

Single machine multi instance MySQL master-slave replication
How does the JVM bottom layer implement synchronized

每日一题: 数组中数字出现的次数

Use and principle of handlerthread

Phoenix安装教程

11.目标分割
随机推荐
Is it safe to open an account on the flush
Typescript -- Section 7 enumeration
Basic use of Chrome browser
Report on the convenient bee Lantern Festival: the first peak sales of pasta products this year; prefabricated wine dumplings became the winners
Leetcode daily question: implementing strstr()
LG. Hankson's interesting questions, C language
TypeScript -- 第一节:基础类型
手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
Typescript -- Section 3: Interface
请问同花顺上开户安全吗
随笔记:定义setter和getter的三种方式
养老年金险是理财产品吗?预期收益在哪看?
6.28 学习内容
Technology sharing | software development process that you must understand if you want to get started with testing
Give you a project, how will you carry out performance testing (I)
Reading notes of English grammar new thinking Basic Edition 2 (I)
The magical zero knowledge proof can not only keep secrets, but also make others believe you!
小白创业做电商,选对商城系统很重要!
11.目标分割
var、let、const 三者的区别