zoukankan      html  css  js  c++  java
  • [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];
            }
  • 相关阅读:
    [更新]一份包含: 采用RSA JWT(Json Web Token, RSA加密)的OAUTH2.0,HTTP BASIC,本地数据库验证,Windows域验证,单点登录的Spring Security配置文件
    fstab文件详解
    Struts2与Spring的Maven依赖冲突
    maven正确的集成命令-U -B 等
    CentOS6安装Jenkins
    CentOS6下Jenkins连接Git服务器出错的问题
    GitLab备份的创建与恢复
    开发App到上架应用市场需要经历什么?
    阅读笔记:A Few useful things to Know About machine Learning
    Feature Tools 简介
  • 原文地址:https://www.cnblogs.com/diegodu/p/4576831.html
Copyright © 2011-2022 走看看