• [LeetCode] Find Minimum in Rotated Sorted Array


    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

    Suppose a sorted array is rotated at some pivot unknown to you beforehand.

    (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

    Find the minimum element.

    You may assume no duplicate exists in the array.

    Hide Tags
     Array Binary Search
     
     
    分析:
    第一:low < high && A[low] > A[high 保证 转折点在 low和high之间
    第二: if(low + 1 == high)     return A[high]; 只有两个元素时,返回A[high],因为A[low] > A[high]
    第三:注意下面code中何时包含mid,何时不包含mdi
                   if(A[low] < A[mid])//pivot is in bottom half, mid can't be lowest
                    {   
                        low = mid + 1;
                    }   
                    else //pivot is in first half, mid may be lowest
                    {   
                        high = mid;
                    }               

    第四:如果没进while循环,说明数组是有序的,没有经过rotate,所以返回A[low] 即可。

     
     
    class Solution {
        public:
            int findMin(vector<int>& A)
            {
                int low = 0;
                int high = A.size() - 1;
    
                int mid = 0;
                // A[low] > A[high] ensure the pivot is between low and high
                while(low < high && A[low] > A[high])
                {   
                    //if there only two elements, just return A[high], find pivot
                    if(low + 1 == high)
                        return A[high];
    
                    mid = (low + high)/2;
    
                    //cout << "low	" << low<< endl; 
                    //cout << "high	" << high << endl; 
                    //cout << "mid	" << mid<< endl; 
    
                    if(A[low] < A[mid])//pivot is in bottom half, mid can't be lowest
                    {   
                        low = mid + 1;
                    }   
                    else //pivot is in first half, mid may be lowest
                    {   
                        high = mid;
                    }   
                }   
    
                // this case: vecotr A is Sorted Array , not rotated, so just return A[low]
                return A[low];
            }   
    };

    略加修改,使其成为https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/的基础

            int findMin(vector<int>& A)
            {   
                int low = 0;
                int high = A.size() - 1;
    
                int mid = 0;
                // A[low] > A[high] ensure the pivot is between low and high
                while(low < high && A[low] > A[high])
                {   
    
                    mid = (low + high)/2;
    
                    //cout << "low	" << low<< endl; 
                    //cout << "high	" << high << endl; 
                    //cout << "mid	" << mid<< endl; 
    
                    if(A[low] < A[mid])//pivot is in bottom half, mid can't be lowest
                    {   
                        low = mid + 1;
                    }   
                    else if(A[low] == A[mid])//there only two elements
                    {   
                        //if there only two elements, just return A[high], find pivot
                        return A[high];
                    }   
                    else //pivot is in first half, mid may be lowest
                    {   
                        high = mid;
                    }   
                }   
    
                // this case: vecotr A is Sorted Array , not rotated, so just return A[low]
                return A[low];
            }
  • 相关阅读:
    git学习记录——基础概念和文件的基本操作
    java 之 Properties 类
    java 之 Map类
    java 之 迭代器
    java 之 Collection类
    java 之日期时间操作
    java 之String类
    如何把ASP.NET MVC项目部署到本地IIS上
    [工具]Visual Studio
    [MVC][Shopping]Copy Will's Code
  • 原文地址:https://www.cnblogs.com/diegodu/p/4576831.html
Copyright © 2020-2023  润新知