您的位置:首页 > 其它

Find Minimum in Rotated Sorted Array

2014-11-03 21:49 155 查看
when the vector is sorted,we can have three part no mater how many times the rotation happens.

so, we can use divided the sorted array in two parts. and choose the mid value of the whole array.

if the mid array < terminal element. the minus value would be the mid or in the 1 part. because the array is ordered.

and if the mid array > terminal. we can easily tell that the minimum is in the 2th part.class Solution {
public:
int findMin(vector<int> &num) {
int length = num.size()-1;
int start = 0;
int terminal = length;
while(num[start]>num[terminal])
{
int mid = (start+terminal)/2;
if(num[mid] < num[terminal])
terminal = mid;
else
start = mid+1;

}
return num[start];
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: