当前位置:网站首页>【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];
}
}
边栏推荐
- Comics | goodbye, postman! One stop collaboration makes apipost more fragrant!
- 转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)
- Pinhole camera with added lens
- TypeScript--第四节:函数
- mysql 高可用双主同步
- Use and principle of handlerthread
- PHP函数file_get_contents与操作系统的内存映射
- oracle 去掉html标签
- Typescript -- Section 5: classes
- Give you a project, how will you carry out performance testing (I)
猜你喜欢

架构实战营|模块5

Matrix compression

Typescript-- section 4: Functions

Leetcode daily question: implementing strstr()
![[image registration] improved SAR image registration based on sar-sift with matlab code](/img/69/4e78c6cef45b2e4d133222a4372e43.jpg)
[image registration] improved SAR image registration based on sar-sift with matlab code

12.物体检测Mask-Rcnn

Trois questions PWN
![[buuctf.reverse] 131-135](/img/c2/b8b06c8191af2c75bf4ad5c82feaea.png)
[buuctf.reverse] 131-135

Excel使用过程中的参考资料

Accessories and working process of machine vision system
随机推荐
剑指 Offer 12. 矩阵中的路径
MapReduce case
大智慧上开户是安全的吗
好用免费的PPT模板
滑环的基本结构及工作原理分析
LG. Hankson's interesting questions, C language
[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)
Accessories and working process of machine vision system
由背景图缓存导致的canvas绘图跨域问题
mysql 8.0以上报2058 解决方式
TypeScript --第三节:接口
Es6:let, const, arrow functions
12. object detection mask RCNN
oracle 去掉html标签
Baidu online disk login verification prompt: unable to access this page, or the QR code display fails, the pop-up window shows: unable to access this page, ensure the web address....
12.物體檢測Mask-Rcnn
Sword finger offer 12 Path in matrix
Typescript -- Section 3: Interface
每日一题:数组中数字出现的次数2
Typescript -- Section 1: basic types