首页 > 代码库 > Find Minimum in Rotated Sorted Array I && II

Find Minimum in Rotated Sorted Array I && II

这些题比较考验边界条件

Find Minimum in Rotated Sorted Array

class Solution {
public:
    int findMin(vector<int> &num) {
        int left = 0, right = num.size()-1;
        while(num[left] > num[right]){
              int mid = (left+right) >> 1;
              if(num[mid] < num[right])
                 right = mid;
              else 
                 left = mid+1;
        }
        return num[left];
    }
};


Find Minimum in Rotated Sorted Array II

class Solution {
public:
    int findMin(vector<int> &num) {
        int left = 0, right = num.size()-1;
        while( num[left] >= num[right]){
              int mid = (left+right) >> 1;
              if(num[mid] < num[right])
                 right = mid;
              else if(num[mid] > num[right])
                 left = mid+1;
             else 
                right--;
        }
        return num[left];
    }
};


Find Minimum in Rotated Sorted Array I && II