LintCode-159: Find Minimum in Rotated Sorted Array
程序员文章站
2022-03-24 17:24:26
...
binary search的变种。先设target=nums[end], 然后mid跟target比,如果nums[mid]>=target,则说明mid切在第2象限,此时扔掉左边边,否则说明mid切在第4象限,此时扔掉右半边。
while()循环完后的最小值的index要么是start,要么是end。再比较一下即可。
int findMin(vector<int> &nums) {
int start=0, end=nums.size()-1;
int target=nums[end];
while(start+1<end) {
int mid=start+(end-start)/2;
if (nums[mid]>=target) start=mid;
else end=mid;
}
if (nums[start]<=nums[end]) return nums[start];
else return nums[end];
}
推荐阅读
-
LeetCode 33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II
-
Leetcode 33 Search in Rotated Sorted Array
-
LeetCode·33. Search in Rotated Sorted Array
-
leetcode 33. Search in Rotated Sorted Array
-
0033_Search in Rotated Sorted Array
-
Search in Rotated Sorted Array II
-
33. Search in Rotated Sorted Array
-
LeetCode------Search in Rotated Sorted Array
-
33. Search in Rotated Sorted Array
-
33. Search in Rotated Sorted Array