• [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];
            }
  • 相关阅读:
    efwplus框架
    注册区域
    社招面试记录与总结
    验证码 Captcha 之大插件
    发生内存泄漏?
    Flume+LOG4J+Kafka
    协议如何保证可靠传输
    oracle之spool详细使用总结(转)
    SSH协议详解(转)
    oracle nologging用法(转)
  • 原文地址:https://www.cnblogs.com/diegodu/p/4576831.html
Copyright © 2020-2023  润新知