`
darren_nizna
  • 浏览: 72762 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

[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.

 

代码:

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

 

  参考:Find Minimum in Rotated Sorted Array


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics