当前位置:网站首页>Sword finger offer 11. rotate the minimum number of the array
Sword finger offer 11. rotate the minimum number of the array
2022-07-25 02:20:00 【[email protected]】

https://leetcode.cn/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/
Solution 1 :
Violence law , Go through the array directly , Find the maximum value
class Solution {
public int minArray(int[] numbers) {
int min=Integer.MAX_VALUE;
for(int number:numbers){
if(number<min){
min=number;
}
}
return min;
}
}
//O(n)
//O(1)
class Solution {
public:
int minArray(vector<int>& numbers) {
int min=INT_MAX;
for(int num:numbers){
if(num<min){
min=num;
}
}
return min;
}
};
//O(n)
//O(1)
class Solution:
def minArray(self, numbers: List[int]) -> int:
min=10000
for num in numbers:
if num<min:
min=num
return min
Solution 2 : Two points search
For the number after rotation , Although the array as a whole is not ordered , But the array is divided into two parts , These two parts must be in order , With numbers = [3,4,5,1,2] For example ,3 4 5 It's in ascending order ,1 2 It's in ascending order , That is, the array is divided into two parts in order , They are respectively called left half order and right part order , The smallest number is the first number in the right half
The rightmost number must belong to the right half of the order , Put the rightmost element nums[r] And intermediate elements num[mid] Compare , Divided into the following 3 In this case :
- nums[r]<nums[mid]: mid It belongs to the left half of the order , The minimum number belongs to the interval [mid+1,r] mid It can't be the smallest number , Therefore, the interval does not contain mid
- nums[r]>nums[mid]: mid It belongs to the right half of the order , The minimum number belongs to the interval [left,mid] mid May be the smallest number , Therefore, the interval contains mid
- nums[r]==nums[mid]: Because there are duplicate elements in the array , Do not return directly at this time , Instead, narrow the right boundary
There is no peace here nums[i] The reason for the comparison is because the leftmost element at the beginning nums[i] It is not certain whether it belongs to the left half of the ordered array or the right half of the ordered array
class Solution {
public int minArray(int[] numbers) {
int left=0,right=numbers.length-1;
while(left<right){
int mid=left+(right-left)/2;
if(numbers[mid]>numbers[right]){
left=mid+1;
}else if(numbers[mid]<numbers[right]){
right=mid;
}else if(numbers[mid]==numbers[right]){
right--;
}
}
return numbers[left];
}
}
//O(logn)
//O(1)
class Solution {
public:
int minArray(vector<int>& numbers) {
int l=0,r=numbers.size()-1;
while(l<r){
int m=l+(r-l)/2;
if(numbers[m]>numbers[r]){
l=m+1;
}else if(numbers[m]<numbers[r]){
r=m;
}else if(numbers[m]==numbers[r]){
r--;
}
}
return numbers[l];
}
};
class Solution:
def minArray(self, numbers: List[int]) -> int:
left=0
right=len(numbers)-1
while left<right:
mid=left+(right-left)//2
if numbers[mid]>numbers[right]:
left=mid+1
elif numbers[mid]<numbers[right]:
right=mid
elif numbers[mid]==numbers[right]:
right-=1
return numbers[left]
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/202/202207200907587180.html
边栏推荐
- Mongodb security cluster construction
- Data management process model of science and technology planning project
- [system design] distributed key value database
- How to upload files to the server
- Project management tool Zen
- Jedispoolconfig parameter configuration from the perspective of source code
- Detailed explanation of manually writing servlet in idea
- JVM Foundation
- Safety management and application of genomic scientific data
- Simulate the implementation of strstr
猜你喜欢

2022.7.20 linear table
![[hero planet July training leetcode problem solving daily] 20th BST](/img/25/2d2a05374b0cf85cf123f408c48fe2.png)
[hero planet July training leetcode problem solving daily] 20th BST
![[system design] distributed key value database](/img/57/4d835d83f0e6ffb87e8ba39ec3b482.png)
[system design] distributed key value database

An article explains unsupervised learning in images in detail

Talk about what's going on with C # backstage GC?

Vs2019 configuring Qt5 development environment

Example demonstration of "uncover the secrets of asp.net core 6 framework" [02]: application development based on routing, MVC and grpc
![[recognize cloud Nativity] Chapter 4 cloud network section 4.9.4.3 - smart network card usage scenario - network acceleration implementation](/img/9a/b69036bf920706360d8b293dad2a89.png)
[recognize cloud Nativity] Chapter 4 cloud network section 4.9.4.3 - smart network card usage scenario - network acceleration implementation

VRRP virtual redundancy protocol configuration

How to use ES6 async and await (basic)
随机推荐
How MySQL 8.0 based on TRX_ Id find the statement of the whole transaction
Android memory optimized disk cache
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
KVM virtualization jobs
Detailed explanation of the principles and differences between static pages and dynamic pages
Promise basic use
QT realizes calendar beautification
When sharing a folder, the 'attempt to share xxxxx
JVM Foundation
[linear DP] Digital triangle
Multithreading and high concurrency (II) -- synchronized locking and unlocking process
July 8, 2022
Please ask a question: how to set the new table of MySQL CDC 2.2.x to only receive increment
Four redis cluster schemes you must know and their advantages and disadvantages
Focus on improving women's and children's sense of gain, happiness and security! In the next ten years, Guangzhou Women's and children's undertakings will make such efforts
Industrial control safety PLC firmware reverse II
Upgrade the leapfrog products again, and the 2023 Geely Xingrui will be sold from 113700 yuan
How to upload files to the server
Vs2019 configuring Qt5 development environment
An article explains unsupervised learning in images in detail