当前位置:网站首页>Sword finger offer | minimum number of rotation array
Sword finger offer | minimum number of rotation array
2022-07-24 03:38:00 【Little Yapi】
There is a length of n Non descending array of , such as [1,2,3,4,5], Rotate it , That is, move the first elements of an array to the end of the array , Into a rotating array , For example, it became [3,4,5,1,2], perhaps [4,5,1,2,3] In this way . Excuse me, , Given such a rotation array , Find the minimum value in the array
Ideas :
Although after rotation , But the first and second paragraphs are orderly , So consider using dichotomy to solve .
send middle = (start+end)/2, take middle and end Compare :
If Greater than , The result is on the left ; If Less than , The result is on the right ;
If be equal to , It is impossible to judge which side ( for example 10111 11101 Both sides are possible ) Need to shrink end Keep looking for .
Why not use the left start and middle Compare ?
for example 12345 3>1 , The minimum value is on the left , 34512 5>3 But the minimum value is on the right . It's impossible to judge .
Code :
public int minNumberInRotateArray(int [] array) {
if(array.length == 0){
return 0;
}
int start = 0;
int end = array.length-1;
while(start < end){
int middle = (start+end)>>>1;
if(array[middle] < array[end]){
end = middle;
}else if(array[middle] > array[end]) {
start = middle + 1;
}else {
end --;
}
}
return array[end];
}
Living without an aim is like sailing without a compass.
—— J. Ruskin
边栏推荐
- 数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
- 正則錶達式 \b \B 深入淺出理解單詞邊界的匹配
- Write code, and multiple characters move from both ends to converge in the middle
- Exttestngireporterlistener all codes
- Cache component status when Vue components are switched, that is, dynamic components keep alive dynamic components and asynchronous components
- Introduction to pytorch ecology
- Paper reading: the perfect match: 3D point cloud matching with smoothed densities
- JS Array isaarray () Type of
- The former backbone of WPS spent 10 years building new software. Excel users: I switched to WPS for this
- B. Eastern Exhibition- Codeforces Round #703 (Div. 2)
猜你喜欢

C file operation details

1. Learn to understand web pages

Realize the communication before two pages (using localstorage)

4. Hezhou air32f103_ LCD

MLP-多层感知机

Jump statements break and continue

QT custom class uses custom parametric signals and slots

buu web

Common properties and traversal of trees and binary trees

Lagrange polynomial
随机推荐
1. Learn to understand web pages
How to realize WiFi Internet short message authentication in the park / factory
08 reptile project
dynamixel舵机在ros下的workbnech使用
jvm类加载过程简介说明
Expressions régulières \ \ B \ \ b compréhension de l'appariement des limites des mots
IDEA Clone的项目报Cannot resolve symbol ‘Override‘
正則錶達式 \b \B 深入淺出理解單詞邊界的匹配
Bingbing learning notes: basic operation of vim tool
21st day of written test mandatory training
IO流分类整理
Simulink code generation: variable subsystem and its code
Tetris, 1
RTOS内功修炼记(十) | 深度解析RTOS内核上下文切换机制
What is IMU?
An in-depth explanation of CAS is necessary for interview practice
Binary search
C. Minimum Ties-Educational Codeforces Round 104 (Rated for Div. 2)
Error code 0x80004005
[interpolation expression of applet, rendering judgment, binding events and sharing]