当前位置:网站首页>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];
}
}
边栏推荐
- 荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
- VM虚拟机 没有未桥接的主机网络适配器 无法还原默认配置
- 解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
- Sangi diagram of machine learning (for user behavior analysis)
- Yadi started to slow down after high-end
- Dynamic planning for stair climbing
- egg-sequelize JS编写
- MySQL only checks the reasons for the slow execution of one line statements
- 力扣每日一题-第42天-661. 图片平滑器
- SwiftUI一日速成
猜你喜欢

LeetCode:1184. 公交站间的距离————简单

Comprehensive evaluation and decision-making method

1. Mx6u-alpha development board (main frequency and clock configuration experiment)

When you try to delete all bad code in the program | daily anecdotes

Soft simulation rasterization renderer

低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!

当你尝试删除程序中所有烂代码时 | 每日趣闻

ASP. Net core actionfilter filter details

How engineers treat open source -- the heartfelt words of an old engineer
![[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
随机推荐
Day24 job
Mantium 如何在 Amazon SageMaker 上使用 DeepSpeed 实现低延迟 GPT-J 推理
Use of rule engine drools
ROS2的launch有何不同?
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
机器学习之信用卡欺诈检测
egg-ts-sequelize-CLI
第三篇如何使用SourceTree提交代码
Compiled by egg serialize TS
Unable to find sygwin.s file during vscode debugging
Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
How to make your language academic when writing a thesis? Just remember four sentences!
Credit card fraud detection based on machine learning
VM virtual machine has no un bridged host network adapter, unable to restore the default configuration
Graph translation model
Integrated architecture of performance and cost: modular architecture
The difference between positive samples, negative samples, simple samples and difficult samples in deep learning (simple and easy to understand)
Graph theory: topological sorting
Wu Enda's machine learning after class exercises - linear regression
p-范数(2-范数 即 欧几里得范数)