码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Find Minimum in Rotated Sorted Array II"

时间:2014-11-15 16:56:14      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   div   on   

Similar with version I, but analysis is different. With the possibility of duplication, only ">" indicate a definite case of next search space.

class Solution 
{
public:    
    int _findMin(vector<int> &num, int s, int e)
    {
        if (s == e) return num[s];
        if ((s + 1) == e)    return std::min(num[s], num[e]); // assumption on input
        
        int mid = s + ((e - s) >> 1);
        int a = num[s], b = num[mid], c = num[mid + 1], d = num[e];
        
        if (a > b) return _findMin(num, s, mid);
        if (c > d) return _findMin(num, mid + 1, e);
        if (b > c) return c;
        
        return std::min(_findMin(num, s, mid), _findMin(num, mid + 1, e));
    }

    int findMin(vector<int> &num) 
    {
        return _findMin(num, 0, num.size() - 1);
    }
};

LeetCode "Find Minimum in Rotated Sorted Array II"

标签:style   blog   io   color   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/tonix/p/4099445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!