首页 > 其他 > 详细

LeetCode Find Minimum In Rotated Sorted Array

时间:2014-11-18 01:33:53      阅读:256      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;
// 4 5 1 2
// 1 2
class 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

原文:http://www.cnblogs.com/lailailai/p/4104736.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!