153. Find Minimum in Rotated Sorted Array leetcode binary search

2017-01-04 19:18:33来源:CSDN作者:shanshanhi人点击

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.

注意:是不存在重复元素的数组

//二分查找的思想    int findMin(vector<int>& nums) {        int low = 0;        int high = nums.size() - 1;                while(low < high)        {            if(nums[low] < nums[high])                return nums[low];                        //进入到以下部分说明不能满足上述的if条件,即以下部分nums[low] > nums[high]            int mid = low + (high - low) / 2;            if(nums[mid] >= nums[low]) //如:数组{1,2}如果不带等号则错误                low = mid + 1;            else                high = mid;        }                return nums[low];    }


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台