[LeetCode] 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.

 

代码:

class Solution {
public:
    int findMin(vector<int> &num) {
        int i = 0, j = num.size() - 1;
        while(i < j) {
            if(num[i] < num[j]) {
                return num[i];
            }
            int m = (i + j) / 2;
            if(num[m] >= num[i]) {
                i = m + 1;
            } else {
                j = m;
            }
        }
        return num[i];
    }
};

 

  参考:Find Minimum in Rotated Sorted Array


 

你可能感兴趣的:(LeetCode)