当前位置:网站首页>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
边栏推荐
- Fraud detection using CSP
- Ecosystem long-term observation data product system
- Large number processing -- use case
- Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
- Use Fiddler to capture apps
- Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
- DNA helped solve the outstanding case 30 years ago. The suspect strangled his girlfriend because he fell in love with his roommate. He was already the CEO of the technology company when he was arreste
- Master jedispoolconfig parameter configuration and learn tuning skills
- Win10 configuring CUDA and cudnn
- Simulate the implementation of strstr
猜你喜欢
![[system design] distributed key value database](/img/57/4d835d83f0e6ffb87e8ba39ec3b482.png)
[system design] distributed key value database
![[leetcode] 2. Add two numbers - go language problem solving](/img/26/9b5df9aedc34238ce816cbf8e72066.png)
[leetcode] 2. Add two numbers - go language problem solving

How can arm access the Internet through a laptop?

"Nowadays, more than 99.9% of the code is garbage!"

R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set
![[programmer interview classic] 01.09 string rotation](/img/d2/7ea9351c31af01665d86f8c6bc3468.png)
[programmer interview classic] 01.09 string rotation

Digital commerce cloud fine chemical industry management platform integrated informatization solution

Detailed explanation of the principles and differences between static pages and dynamic pages

An article explains unsupervised learning in images in detail

How to use ES6 async and await (basic)
随机推荐
1260. Two dimensional grid migration: simple construction simulation problem
HAC cluster is modified to stand-alone
Digital business cloud: how to realize the application value of supplier SRM management system?
Can PostgreSQL CDC only connect to the main database? Connection from the library reports an error logical decoden
Inventory of well-known source code mall systems at home and abroad
Jedispoolconfig parameter configuration from the perspective of source code
Academicians said: researchers should also support their families. They can only do short-term and fast research if they are not promoted
Simulate the implementation of StrCmp function
It's still a synchronization problem
July 8, 2022
Beijing Zhun electric clock, Beidou clock server, GPS network time server, NTP satellite timing system
QT realizes calendar beautification
Eolink - quickly develop interfaces through document driven
Application status of typical marine environmental observation data products and Its Enlightenment to China
Genesis, the world's first "consumption investment" public chain, was invited to participate in consensus 2022
What are the important trends revealed by the release of "operator data viability index"?
Digital commerce cloud fine chemical industry management platform integrated informatization solution
How to upload files to the server
Jsonp solves cross domain plug-ins (JS, TS)
How to use ES6 async and await (basic)