LeetCode - Find Minimum in Rotated Sorted Array II

二分查找

 1 class Solution {
 2 public:
 3     int findMin(vector<int> &num) {
 4         int n = num.size();
 5         if (n == 1)
 6             return num[0];
 7         int first = 0, last = n-1;
 8         while (first < last && num[first] >= num[last])
 9         {
10             int mid = (first+last)/2;
11             if (num[mid] > num[last])
12                 first = mid + 1;
13             else if (num[mid] < num[first])
14                 last = mid;
15             else
16                 first++;
17         }
18         return num[first];
19     }
20 };

 

posted @ 2015-04-03 00:14  bournet  阅读(101)  评论(0编辑  收藏  举报