首页 > 其他 > 详细

LeetCode – Refresh – Find Minimum in Rotated Array

时间:2015-03-19 09:59:31      阅读:317      评论:0      收藏:0      [点我收藏+]

Be carefully with one element and two element sitution.

1. Since mid = (start + end) / 2 is alway shifting to the left. So when we do comparision, not use num[mid] > num[start] but num[mid] > num[end].

2. We need find the minimum, So when we are closing to the minimum, we need get start = mid + 1 and end = mid as offset for left shifting.

 

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

 

LeetCode – Refresh – Find Minimum in Rotated Array

原文:http://www.cnblogs.com/shuashuashua/p/4349503.html

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