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];
}
};
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];
}
};
相关文章推荐
- Leetcode - "Min stack" "Find Minimum in Rotated sorted Array"
- leetcode Find minimum in rotated sorted array
- Find Minimum in Rotated Sorted Array
- Leetcode-Find Minimum in Rotated Sorted Array II
- Find Minimum in Rotated Sorted Array
- [LeetCode] Find Minimum in Rotated Sorted Array 题解
- leetcode 之Find Minimum in Rotated Sorted Array
- [LeetCode]Find Minimum in Rotated Sorted Array II
- LeetCode 之 Find Minimum in Rotated Sorted Array II
- Find Minimum in Rotated Sorted Array II -- leetcode
- 153. Find Minimum in Rotated Sorted Array
- Find Minimum in Rotated Sorted Array && Find Minimum in Rotated Sorted Array II
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
- 【leetcode】【153】Find Minimum in Rotated Sorted Array
- LeetCode——Find Minimum in Rotated Sorted Array
- Leetcode|Find Minimum in Rotated Sorted Array II(有重复元素的二分查找)
- [leetcode]Find Minimum in Rotated Sorted Array II
- 【LEETCODE】153-Find Minimum in Rotated Sorted Array
- lintcode-medium-Find Minimum in Rotated Sorted Array
- LeetCode(153) Find Minimum in Rotated Sorted Array