当前位置:网站首页>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;
}边栏推荐
- 头文件库文件
- It's settled! On July 30!
- ️雄关漫道真如铁,而今迈步从头越️
- SQL Server、MySQL主从搭建,EF Core读写分离代码实现
- 【FPGA教程案例41】图像案例1——通过verilog读取图片
- Edge team explains how to improve the comprehensive performance experience through disk cache compression technology
- 基于ModelArts续写最伟大的作品【玩转华为云】
- leetcode076——数组中的第 k 大的数字
- TCP Basics
- In the era of home health diagnosis, Senzo creates enhanced lateral flow test products
猜你喜欢

OSPF expansion configuration, routing principles, anti ring and re release

关于软考高级要不要报班学习

Redis面试题必知必会
![[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC](/img/16/e5aa43df6ef9bdbc173fa547c85559.png)
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC

Read Plato farm's eplato and the reason for its high premium

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

Skillfully use NGX_ Lua makes traffic grouping

二分、三分、01分数规划【第III弹】

博弈论 1.Introduction(组合游戏基本概念、对抗搜索、Bash游戏、Nim游戏)

Illustrate three mainstream enterprise architecture models (recommended collection!)
随机推荐
谈谈基于JS实现阻止别人调试通过控制台调试网站的问题
Weekly report on July 27, 2022
QT | some summaries of signals and slots
图解 3 种主流企业架构模式(建议收藏!)
二分、三分、01分数规划【第III弹】
uni-app进阶之创建组件/原生渲染
SQL server, MySQL master-slave construction, EF core read-write separation code implementation
Installing MySQL for Linux operating system (centos7)
高温天气筑牢安全生产防线,广州海珠区开展加油站应急演练
软件设计师考前20问,注意啦!!
How PHP gets the interface
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
19. 删除链表的倒数第 N 个结点
什么样的知识付费系统功能,更有利于平台与讲师发展?
ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
Sizebasedtriggingpolicy introduction
基于docker安装MySQL
关于软考高级要不要报班学习
02.1.2.逻辑类型 bool
vc链接静态库的时候要注意的问题