当前位置:网站首页>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
边栏推荐
- Safety management and application of genomic scientific data
- Today in history: the kotlin language was published for the first time; The father of the IMAP agreement was born; CT imaging achieves a new breakthrough
- Componentization and modularization
- After six years of precipitation, new consumption has found a digital transformation paradigm
- 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
- High performance memory recovery technology for decrypting ark -- HPP GC
- When sharing a folder, the 'attempt to share xxxxx
- Query the thread information of MySQL server (detailed explanation)
- Digital commerce cloud fine chemical industry management platform integrated informatization solution
- Create thread: pthread_ create
猜你喜欢

Eolink - quickly develop interfaces through document driven

Use Fiddler to capture apps

After six years of precipitation, new consumption has found a digital transformation paradigm

B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry

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

BMW I3 based on clar architecture is not a simple "oil to electricity" product

Peripherals: interrupt system of keys and CPU
![Deamnet|filenotfounderror: [winerror 3] the system cannot find the specified path.: '/ Datasettest\\Set12‘](/img/7c/0af10dceb20c0b392d0bab749eb355.png)
Deamnet|filenotfounderror: [winerror 3] the system cannot find the specified path.: '/ Datasettest\\Set12‘

Is it necessary to increase the number of milliseconds and save several KB of memory in the program?

A bit of knowledge - websites about scripts
随机推荐
Application status of typical marine environmental observation data products and Its Enlightenment to China
Scalar, vector, matrix calculus
Safety management and application of genomic scientific data
Eolink - quickly develop interfaces through document driven
"I gave up programming and wrote a 1.3 million word hard science fiction."
Research and application of scientific data management strategy for high energy synchrotron radiation source
Digital power supply -- Chapter 1
AWD thinking
Detailed explanation of the principles and differences between static pages and dynamic pages
Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil
Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
Redis tutorial
Talk about what's going on with C # backstage GC?
Creating elements of DOM series
Yunyuanyuan (VIII) | Devops in depth Devops
Gerrit statistics script
JS utils tool function that makes you get twice the result with half the effort
Thinkphp5.0.24 deserialization chain analysis
Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
6-11 vulnerability exploitation - use the built environment to send emails