当前位置:网站首页>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
边栏推荐
- Thinkphp5.0.24 deserialization chain analysis
- MySQL advanced (13) command line export import database
- 2022.7.20 linear table
- Babbitt | metauniverse daily must read: Dubai launched the national metauniverse strategy, which plans to increase the number of related companies of metauniverse by five times in the next five years
- Summary thinking caused by the function of a SMS verification code [easy to understand]
- Inventory of well-known source code mall systems at home and abroad
- C traps and defects Chapter 2 lexical "traps" 2.4 switch statements
- Hongmeng harmonyos 3 official announcement: officially released on July 27; Apple slowed down the recruitment and expenditure of some teams in 2023; Russia fined Google 2.6 billion yuan | geek headlin
- Talk about resume optimization and interview skills of algorithm post!
- POM reports an error
猜你喜欢

See you in Suzhou! "Cloud Intelligence Technology Forum - industry special" will be held soon

Why can't reading more books improve your writing?

Data integration | what are the tools for data integration at home and abroad?

Industrial control safety PLC firmware reverse III

iptables :chains, target

My creation anniversary (3rd Anniversary)

Fraud detection using CSP

Nacos service discovery data model

Multithreading and high concurrency (II) -- synchronized locking and unlocking process

Full analysis of new functions of report design tool FastReport online designer v2022.1
随机推荐
JVM Foundation
When executing SQL query statements in MySQL database, the underlying implementation principle (ultra detailed)
Redis tutorial
Dynamic memory development
Gerrit statistics script
H5 common positioning function package
Eolink - quickly develop interfaces through document driven
Completefuture parallel asynchronous return processing
JS utils tool function that makes you get twice the result with half the effort
Simulation Implementation of [STL] string class
QT realizes calendar beautification
Download files and web pages with WGet
Introduction to ORM framework - what is ORM framework?
Digital commerce cloud fine chemical industry management platform integrated informatization solution
Google launched another "man grabbing war" for core making, and Intel's 17 year veteran joined!
Four redis cluster schemes you must know and their advantages and disadvantages
What are the basic skills of engineers? How to practice? -- Learning experience sharing "suggestions collection"
"No such plugin: cloudbees folder" solution appears when Jenkins selects the recommended plug-in for the first time
Start to build a three node Eureka cluster
Example demonstration of "uncover the secrets of asp.net core 6 framework" [02]: application development based on routing, MVC and grpc