Leetcode 153 Find Minimum in Rotated Sorted Array

2018-01-12 14:51:13 浏览数 (1)

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.

错位的有序数组,找出最小元素

二分,没有什么太值得将的

代码语言:javascript复制
class Solution {
public:
    int findMin(vector<int>& nums) {
        int l = 0, r = nums.size()-1;
        while(nums[l]>nums[r])
        {
            int mid = (l   r) >> 1;
            if(nums[mid] >= nums[l]) 
                l = mid 1;
            else  
                r = mid; 
        }
        return nums[l];
    }
};

0 人点赞