当前位置:网站首页>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;
}边栏推荐
- 谈谈基于JS实现阻止别人调试通过控制台调试网站的问题
- pkg打包node工程
- 巧用ngx_lua做流量分组
- 不登高山,不知天之高也;不临深溪,不知地之厚也
- Read Plato farm's eplato and the reason for its high premium
- CloudCompare&PCL 匹配点采样一致性抑制
- 并查集
- Platofarm has made continuous progress, and has launched the official version and super primitive NFT successively
- (十)defer关键字
- [openharmony] [rk2206] build openharmony compiler (2)
猜你喜欢

Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control

MySQL架构原理

2022-uni-app解析token标准的方式-使用jsrsasign-爬坑过了

高温天气筑牢安全生产防线,广州海珠区开展加油站应急演练

Skillfully use NGX_ Lua makes traffic grouping

记录一次idea中的父子项目修改project与module名称,亲测!

API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
![[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9](/img/39/8efef047d0a9223b97819a54b5edf8.png)
[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9

Choosing a supplier service system is the first step for large health industry enterprises to move towards digital transformation

Installing MySQL for Linux operating system (centos7)
随机推荐
LinkedList source massage, ah comfortable
office2013以上输入数学公式
In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations
vc链接静态库的时候要注意的问题
What are the highlights of B2B2C system? How to help jewelry enterprises build an omni channel multi merchant mall management system
Skiasharp's WPF self drawn drag ball (case version)
【云驻共创】华为云:MetaStudio数字内容生产线,让虚拟世界与现实世界无缝融合
[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals
[openharmony] [rk2206] build openharmony compiler (2)
Several innovative economic models of platofarm have inspired the current metacosmic market
Winform 生成随机验证码
On July 13, 2021, we collapsed like this
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC
SQL server, MySQL master-slave construction, EF core read-write separation code implementation
二分、三分、01分数规划【第III弹】
二维前缀和
Prometheus operation and maintenance tool promtool (IV) TSDB function
双指针技巧
巧用ngx_lua做流量分组
B2B e-commerce website scheme for building materials industry: enable the transformation and upgrading of building materials enterprises to achieve cost reduction and efficiency improvement