题解 | #旋转数组的最小数字#

旋转数组的最小数字

https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba

class Solution {
public:
    int minNumberInRotateArray(vector<int>& nums) {
        if(nums.size() == 0) return -1;

        int left = 0, right = nums.size()-1;
        while(left < right){
            if(nums[left] < nums[right]){
                return nums[left];
            } 

            int mid = left + (right-left)/2;
            if(nums[mid] > nums[right]){
                left = mid+1;
            }
            else if(nums[mid] < nums[right]){
                right = mid;
            }
            else if(nums[mid] == nums[right]){
                right--;
            }
        }
        return nums[left];
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务