leetcode 153.寻找旋转排序数组中的最小值(find minimum in rotated sorted array)C语言
1.description
https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
请找出其中最小的元素。
你可以假设数组中不存在重复元素。
示例 1:
输入: [3,4,5,1,2]
输出: 1
示例 2:
输入: [4,5,6,7,0,1,2]
输出: 0
2.solution
通过这个题,才知道二分查找原来有这么多学问,更多的时间花在了学习大佬的总结,见leetcodecn题解1,2
int findMin(int* nums, int numsSize){
int low = 0, high = numsSize-1;
while(low < high){
int mid = low + ((high - low)>>1);
if(nums[mid] > nums[high]){
low = mid+1;
}else{ // nums[high] > nums[mid], 不会出现等于, 因为low<high, 而mid向下取整, 一定小于high
high = mid;
}
}
return nums[low];
}