首页 > 代码库 > LeetCode Find Minimum In Rotated Sorted Array

LeetCode Find Minimum In Rotated Sorted Array

#include <iostream>#include <cstdlib>#include <vector>using namespace std;// 4 5 1 2// 1 2class Solution {public:    int findMin(vector<int> &num) {        int len = num.size();        if (len < 1) {            // should not happened;            return 0;        }        if (len == 1) {            return num[0];        }         int p = 0;         int q = len - 1;                // array with no rotation        if (num[p] < num[q]) {            return num[p];        }                // array been rotated        while (p + 1 < q) {            int m = (p + q) / 2;            if (num[m] > num[q]) {                p = m;            } else if (num[m] < num[q]){                q = m;            }        }        return num[q];    }};int main() {    //int arr[] = {4, 5, 6, 7, 0, 1, 2};    //int arr[] = {1, 2, 3};    int arr[] = {1, 2};    //int arr[] = {2, 1};    vector<int> array(arr, arr + sizeof(arr) / sizeof(int));        Solution s;        cout<<s.findMin(array)<<endl;            return 0;}

 好像已经用这题,重复添加了

LeetCode Find Minimum In Rotated Sorted Array