当前位置:网站首页>Leetcode -- minimum number of rotation array
Leetcode -- minimum number of rotation array
2022-07-28 10:12:00 【Schuyler_ yuan】
subject :
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.
Example 1:
Input :[3,4,5,1,2] Output :1Example 2:
Input :[2,2,2,0,1] Output :0
Ideas :
How do you look at this question , Is to find the minimum value in the array , There are many solutions for finding the minimum , The simplest is to use a min Function done , But you can't do that in an interview , This question has a better idea , Binary search .
Why is binary search ?
Array in question , The first is the rotation of the ordered array , There is a certain degree of order , There is another key feature , The first element will be greater than or equal to the last element , Take this condition as the entry of the loop , Conditions for exiting the loop , When the high and low pointers meet , I found the smallest one .
Of course, there are several special cases ,
1. An ascending array that has not been rotated , At this time, the smallest is the first element ;eg1.[1,3,5]
2. The boundary condition when there is only one element ;eg2.[1]
3. When there are many repetitions , You have to return to the original order to find .eg3.[3,1,3,3,3]
The code is as follows ,
int minArray(vector<int>& numbers) {
int start = 0, end = numbers.size() - 1;
int mid = start; // For the case of ascending order without rotation , Directly return the initial value , That's the minimum
while (numbers[start] >= numbers[end]) {
if (end-start == 0){ // The boundary condition when there is only one element
mid = end;
break;
}
if (end - start == 1) { // Conditions for exiting the loop , When the high and low pointers meet , I found the smallest one
mid = end;
break;
}
mid = (start + end) / 2;
if (numbers[start] == numbers[end] && numbers[mid] == numbers[end]) {
return order(numbers, start, end); // When there are many repetitions , You have to return to the original order to find
}
if (numbers[mid] > numbers[end])
start = mid;
else if (numbers[mid] <= numbers[end])
end = mid;
}
return numbers[mid];
}
int order(vector<int>& numbers, int start, int end) {
int result = numbers[start];
for (int i = start + 1; i <= end; i++) {
if (numbers[i] < result) {
result = numbers[i];
}
}
return result;
}边栏推荐
- 10分钟快速入门EVS【玩转华为云】
- Status Notice ¶
- Introduction to timebasedrollingpolicy
- LandingSite电子标签Quuppa固件进入DFU状态说明
- Skillfully use NGX_ Lua makes traffic grouping
- QT | some summaries of signals and slots
- 13 probability distributions that must be understood in deep learning
- 【MySQL】查询多个ID返回字符串拼接
- Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control
- LIBCMTD.lib
猜你喜欢

二维前缀和

Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble

Guangzhou metro line 14 xinshixu station is under construction, and residents in Baiyun District are about to start a double line transfer mode!

【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03

巧用ngx_lua做流量分组
JWT login authentication + token automatic renewal scheme, well written!

In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations

B2B2C系统亮点是什么?如何助力珠宝首饰企业打造全渠道多商户商城管理体系

Redis面试题必知必会

Redis interview questions must be known and learned
随机推荐
【学习笔记】border与period
[jzof] 14 cut rope
Prometheus operation and maintenance tool promtool (IV) TSDB function
Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control
Installing MySQL for Linux operating system (centos7)
Illustrate three mainstream enterprise architecture models (recommended collection!)
It can traverse all files and subfolders under a folder
LandingSite电子标签Quuppa固件进入DFU状态说明
Read Plato farm's eplato and the reason for its high premium
Status Notice ¶
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC
JS promotion: the underlying principle of flat tiling
Basic examples that must be mastered by beginners of C #
Redis设计规范
21. 合并两个有序链表
语音聊天app——如何规范开发流程?
Prometheus 运维工具 Promtool (四)TSDB 功能
Consul
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
Being on duty less than 8 hours a day and being dismissed? Tencent's former employees recovered 13million overtime pay, etc., and the court won a compensation of 90000 in the final judgment