首页 > 其他 > 详细

[leetcode]Find Minimum in Rotated Sorted Array II

时间:2015-01-01 00:00:09      阅读:353      评论:0      收藏:0      [点我收藏+]

二分,各种情况。

class Solution {
public:
    int findMin(vector<int> &num) {
        int size = num.size();
        int minVal = num[size-1];
        findMinRe(num, 0, size - 1, minVal);
        return minVal;
    }
    
    void findMinRe(vector<int> &num, int left, int right, int &minVal) {
        if (right < left) return;
        int mid = (left + right) / 2;
        if (num[left] < num[right]) {
            minVal = min(minVal, num[left]);
            return;
        }
        if (num[left] > num[right]) {
            minVal = min(minVal, num[right]);
            if (num[mid] <= num[right]) {
                minVal = min(minVal, num[mid]);
                findMinRe(num, left, mid - 1, minVal);
            } else {
                findMinRe(num, mid + 1, right, minVal);
            }
        }
        if (num[left] == num[right]) {
            minVal = min(num[left], minVal);
            if (num[mid] < num[right]) {
                minVal = min(minVal, num[mid]);
                findMinRe(num, left, mid - 1, minVal);
            } else if (num[mid] > num[right]) {
                findMinRe(num, mid + 1, right, minVal);
            } else { // ==
                findMinRe(num, left, mid - 1, minVal);
                findMinRe(num, mid + 1, right, minVal);
            }
        }
    }
};

  

[leetcode]Find Minimum in Rotated Sorted Array II

原文:http://www.cnblogs.com/lautsie/p/4196789.html

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