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.

问题链接

cpp代码如下:

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


猜你喜欢

转载自blog.csdn.net/wocaoqwerty/article/details/41698711
今日推荐