Rotated Sorted Array Series Problems

LC33 Search in Rotated Array
LC81 Search in ROtated Array2
LC153 Find Minimum in Rotated Sorted Array
LC154 Find Minimum in Rotated Sorted Array2

We all know that even though the array is rotated in some pivot, that it still contains sorted information for we can use instead of threat them like unsorted array. so Binary Seach will be used in all those solutions.

and you have to keep in mind, that each time, if we split the array into two parts by the middle, there is always one part that is perfectly sorted. we can judge which part is sorted and then judge our target is in sorted part or not sorted part.

LC33 we need to search a target in a rotated array contains no duplicate values.

class Solution {
    public int search(int[] nums, int target) {
        if(nums == null || nums.length == 0) return -1;
        
        int left = 0;
        int right = nums.length - 1;
        while(left <= right) { //can left and right meet in this circumstances?
            int mid = (right - left) / 2 + left;
            //we have three subcases: when mid is targer/when right part is sorted/when left part is sorted
            if(nums[mid] == target) {
                return mid;
            } else if (nums[mid] <= nums[right]) { //right part is sorted, and we have to add the "="  because mid could be equals to right
                if(target > nums[mid] && target <= nums[right]) {  //if target is in sorted part
                    left = mid + 1; //we move left pointer to right
                } else { //if not, we move right
                    right = mid - 1;
                }
            } else if(nums[mid] >= nums[left]) { 
                if(target < nums[mid] && target >= nums[left]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } 
        }
        return -1;
    }
}

LC81 we need to search a target in a rotated array contains duplicate values.

class Solution {
    public boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return false;
        }
        
        int l = 0;
        int r = nums.length - 1;
        
        while (l <= r) {
            int mid = (r - l) / 2 + l;
            if (nums[mid] == target) {
                return true;
            }
            
            //two subcases: if left part is sorted/if right part is sorted/if we can't sure left part or right part is sorted
            if (nums[l] < nums[mid]) {
                if (target >= nums[l] && target < nums[mid]) { //if target is in the sorted part(which is left part)
                    r = mid - 1;
                } else { //if taeget is not in the sorted part
                    l = mid+1;
                }
            } else if (nums[l] > nums[mid]) {
                if (target > nums[mid] && target <= nums[r]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            } else {
                l++;
            }
        }
        return false;
        
    }
}

LC153 Find Minimum in Rotated Sorted contains non-duplicates Array.
of course we can use linear search, if there is a place where nums[i] > nums[i+1], then return nums[i+1]. if there is no such place, then return nums[0].
but we can convert this idea into binary search, because binary search is to search a target(or number will restricted rules), and in this case, we need to search a place of index = i where nums[i]>nums[i+1].

class Solution {
    public int findMin(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        int left = 0;
        int right = nums.length - 1;
        
        while (left < right) {
            int mid = (right - left) / 2 + left;
            //two cases: right part is sorted or left part is sorted
            if (nums[mid] < nums[right]) { //if right part is sorted, then we definitly can't find minimum in [mid+1, right] part
                right = mid;
            } else { //if left part is sorted, then it means we definitly can't find min in [left, mid] part
                left = mid + 1;
            }
        }
        return nums[left];
    }
}

LC154 find the minimum element in a rotated array, and this array contains duplicates.


class Solution {
    public int findMin(int[] nums) {
        if(nums == null || nums.length == 0) return -1;
        
        int start = 0;
        int end  = nums.length - 1;
        while(start < end){
            int mid = (end - start) / 2 + start;
            //we have this general idea: unsorted part will always have the minimum value
            if (nums[mid] < nums[end]) { //[mid, end] is sorted, so any value from [mid + 1, end] will not be potential minimum value
                start = mid;
            } else if (nums[mid] > nums[end]) { //if [mid, end] is not sorted, then [left, mid] is sorted, so left part can't contains any potential minimum
                end = mid + 1;
            } else { //if nums[mid] == nums[end], that means we can't sure about anything, the only thing we can do is to move end forward by 1 step
                end--;
            }
        }
        return nums[start]; //so ater previous while loop, where the start pointer located at, is where the minimum value is
        
    }
}
posted @ 2020-06-11 22:42  EvanMeetTheWorld  阅读(17)  评论(0)    收藏  举报