1, 154. Look for the minimum value in the rotated sorted array II
It is assumed that an array sorted in ascending order is rotated at a predetermined point in the unknown.
(for example, the array [0,1,2,4,5,6,7]
may become [4,5,6,7,0,1,2]
).
Find the smallest element in the list.
Note that there may be duplicate elements in the array.
classSolution { Public: intFindmin (vector<int>&nums) { if(Nums.empty ())return-1; intLow =0; intHigh = Nums.size ()-1; while(low<=High ) { intMid = (Low+high)/2; if(nums[mid]>Nums[high]) Low= mid+1; Else if(nums[mid]<Nums[high]) high=mid; Else High--; } returnNums[low]; }};
2, 33, search rotation sorted array
It is assumed that an array sorted in ascending order is rotated at a predetermined point in the unknown.
(for example, the array [0,1,2,4,5,6,7]
may become [4,5,6,7,0,1,2]
).
Searches for a given target value, returns its index if the target value exists in the array, otherwise returns -1
.
You can assume that there are no duplicate elements in the array.
Your algorithm's time complexity must be O(log n) level.
Example 1:
Input: Nums = [ 4,5,6,7,0,1,2]
, target = 0 Output: 4
Example 2:
Input: Nums = [ 4,5,6,7,0,1,2]
, target = 3 output:-1
Idea: The key to the binary search method is to get the middle number, judging the following to search the left or right half of the paragraph, we observe that the red numbers above are ascending, so we can observe the law, if the middle number is less than the rightmost number, then the right half is ordered, if the middle number is greater than the rightmost number, then the left half is ordered, We only need to use the first two arrays in the ordered half to determine if the target value is within this area, so we can determine which half is retained,
classSolution { Public: intSearch (vector<int>& Nums,inttarget) { if(nums.size () = =0) return-1; intLow =0, high = Nums.size ()-1; while(low<=High ) { intMid= (Low+high) *0.5; if(Nums[mid] = =target)returnmid; Else if(Nums[mid] >Nums[high])//The middle value is greater than the rightmost number, then the left half is ordered {if(Nums[low] <= target && Nums[mid] >target) high= mid-1; Else Low= mid+1; } Else { if(Nums[mid] < target && Nums[high] >=target)///median is less than the rightmost number, the right half is an ordered low= mid+1; Else High= mid-1; } } return-1; }};
3, 81. Search rotation sorted Array II
It is assumed that an array sorted in ascending order is rotated at a predetermined point in the unknown.
(for example, the array [0,0,1,2,2,5,6]
may become [2,5,6,0,0,1,2]
).
Write a function to determine whether a given target value exists in the array. Returns if there true
is a return false
.
Example 1:
Input: Nums = [2 ,5,6,0,0,1,2]
, target = 0 Output: True
Example 2:
Input: Nums = [2 ,5,6,0,0,1,2]
, target = 3 output: false
Advanced:
- This is the extension of the search rotation sorted array, which
nums
may contain repeating elements.
- Does this affect the time complexity of the program? What kind of impact, why?
Now the array is allowed to repeat the number, this will also affect which side we choose to continue the search, because the previous question does not exist the same value, we compare the median and the rightmost value is fully consistent with the previous rule: if the middle number is less than the rightmost number, then the right half is ordered, if the middle number is greater than the rightmost number, Then the left half is ordered . And if there can be duplicate values, there will be two cases, [3 1 1] and [1 1 3 1], for both cases the middle value equals the rightmost value, the target value 3 can be both on the left and the right, then what to do, for this case is actually very simple, as long as the right-most left one can continue to loop, If it is the same, move on until you move to a different value, and then the other part also uses the search in rotated Sorted array for searching in the rotated ordered array.
classSolution { Public: BOOLSearch (vector<int>& Nums,inttarget) { intres =false; if(Nums.empty ())returnRes; intLow =0; intHigh = Nums.size ()-1; while(Low <=High ) { intMid = (Low+high)/2; if(Nums[mid] = =target)return true; Else if(nums[mid]>Nums[high]) { if(Nums[mid] >target && Nums[low] <=target) high= mid-1; Else Low= mid+1; } Else if(Nums[mid] <Nums[high]) { if(Nums[mid] < target && Nums[high] >=target) low= mid+1; Else High= mid-1 ; } Else High--; } returnRes; }};
Code title (20)-rotate array to find value