当前位置:网站首页>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];
}
}
边栏推荐
- 文献|关系研究能得出因果性结论吗
- When you try to delete all bad code in the program | daily anecdotes
- 【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
- `Oi problem solving ` ` leetcode '2040. The k-th minor product of two ordered arrays
- Recommendation Book Educational Psychology: a book for tomorrow's teachers~
- 零售连锁门店收银系统源码管理商品分类的功能逻辑分享
- Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130
- Firewall command simple operation
- Share | 2022 big data white paper of digital security industry (PDF attached)
- p-范数(2-范数 即 欧几里得范数)
猜你喜欢

性能和成本的综合架构:单元化架构

Segger embedded studio cannot find xxx.c or xxx.h file

Makefile knowledge rearrangement (super detailed)

文献|关系研究能得出因果性结论吗

Pat class a 1039 course list for student

青少年创客教育的创意设计原理

Keil v5安装和使用

Acwing brush questions

Sangi diagram of machine learning (for user behavior analysis)

makefile知识再整理(超详细)
随机推荐
Li Kou daily question - day 42 -661. Picture smoother
Tutorial on using the one click upgrade function of the rtsp/onvif protocol video platform easynvr service
SEGGER Embedded Studio找不到xxx.c或者xxx.h文件
MySQL的优化分析及效率执行
Graph translation model
旋转数组最小数字
The era of smart clothes has come. Zhinar invites you to discuss swords in Yangcheng. Guangya Exhibition is waiting for you on August 4
Keil V5 installation and use
生活相关——一个华科研究生导师的肺腑之言(主要适用于理工科)
ROS2的launch有何不同?
Page pull-up refresh and pull-down loading
[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
零售连锁门店收银系统源码管理商品分类的功能逻辑分享
I.MX6U-系统移植-6-uboot图形化配置
香甜的黄油
LeetCode:1184. 公交站间的距离————简单
解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
Write a paper for help, how to write the discussion part?
How to write the summary? (including 7 easily ignored precautions and a summary of common sentence patterns in 80% of review articles)
Integrated architecture of performance and cost: modular architecture