当前位置:网站首页>Rotate array minimum number
Rotate array minimum number
2022-07-26 04:23:00 【Qingqing not bald】
Move the first elements of an array to the end of the array , We call it rotation of arrays .
Give you a chance to exist repeat An array of element values numbers , It turns out to be an ascending array , And a rotation is carried out according to the above situation . Please return the smallest element of the rotation array . for example , Array [3,4,5,1,2] by [1,2,3,4,5] A rotation of , The minimum value of the array is 1.
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]] .
Example 1:
Input :numbers = [3,4,5,1,2]
Output :1
Example 2:
Input :numbers = [2,2,2,0,1]
Output :0
There are three situations to consider :
array[mid] > array[high]
What happens is array similar [3,4,5,6,0,1,2], At this point, the minimum number must be in mid To the right of
low = mid + 1
array[mid] < array[high]
What happens is array similar [2,2,3,4,5,6,6], The minimum number must be array[mid] Or in mid Left side . Because the right side must be increasing
high = mid
array[mid] == array[high]
What happens is array similar [1,0,1,1,1] perhaps [1,1,1,0,1], At this time, the minimum number is difficult to judge mid Left or right , I have to try one by one
high = high - 1
public class Solution {
public int minNumberInRotateArray(int [] array) {
int low = 0;
int high = array.length - 1;
int mid = (low + high) / 2;
while(low < high) {
// At this point, the minimum number must be in mid To the right of
if(array[mid] > array[high]) {
low = mid + 1;
// The minimum number must be array[mid] Or in mid Left side
} else if(array[mid] < array[high]) {
high = mid;
// Cannot judge in mid Left or right , At this time, we have to try one by one
} else {
high--;
}
mid = (low + high) / 2;
}
return array[low];
}
}
边栏推荐
- Dijikstra (preprocessing first) +dfs, relocation truncated to fit
- I.MX6U-系统移植-6-uboot图形化配置
- Can literature | relationship research draw causal conclusions
- MySQL usage
- Use of rule engine drools
- Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
- Yadi started to slow down after high-end
- What are the duplicate check rules for English papers?
- How to write the abbreviation of the thesis?
- MATLAB绘图
猜你喜欢

解析Steam教育的课程设计测评体系

MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection

1. Mx6u system migration-6-uboot graphical configuration

What if win11 cannot wake up from sleep? Solution of win11 unable to wake up during sleep

Tutorial on using the one click upgrade function of the rtsp/onvif protocol video platform easynvr service

Credit card fraud detection based on machine learning
![[project chapter - how to write and map the business model? (3000 word graphic summary suggestions)] project plan of innovation and entrepreneurship competition and application form of national Entrep](/img/e8/b115b85e2e0547545e85b2058a9bb0.png)
[project chapter - how to write and map the business model? (3000 word graphic summary suggestions)] project plan of innovation and entrepreneurship competition and application form of national Entrep

What are the consequences and problems of computer system restoration

PathMatchingResourcePatternResolver解析配置文件 资源文件

Design and implementation of smart campus applet based on cloud development
随机推荐
Sangi diagram of machine learning (for user behavior analysis)
Function knowledge points
What are the consequences and problems of computer system restoration
Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200
MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection
Day26 job
远坂凛壁纸
Matlab drawing
How to write the introduction and conclusion of an overview paper?
低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!
Recommendation | scholar's art and Tao: writing papers is a skill
dijango学习
吴恩达机器学习课后习题——逻辑回归
Pathmatchingresourcepatternresolver parsing configuration file resource file
Compiled by egg serialize JS
Write a paper for help, how to write the discussion part?
Small ball and box model, arrangement and combination
MySQL的优化分析及效率执行
PathMatchingResourcePatternResolver解析配置文件 资源文件
动态规划 爬楼梯