【leetcode】153. 寻找旋转排序数组中的最小值

 

int findMin(int* nums, int numsSize){
    if (numsSize==1) 
        return *nums;    
    int left=0, right=numsSize-1, mid;
    while(left<right){
        mid=(left+right)/2;
        if(mid>0 && mid<=numsSize-1){
            if (mid<numsSize-1 && nums[mid-1]>nums[mid] && nums[mid+1]>nums[mid])
                return nums[mid];
            else if(nums[mid] > *nums)
                left = mid+1;
            else if(nums[mid] < *nums)
                right= mid-1;
        }
        else if (mid==0){
            if (nums[mid]<nums[mid+1])
                return *nums;
            else
                left=mid+1;
        }
    }
    return (nums[left]<*nums)?nums[left] :*nums;
}

 

posted @ 2020-12-24 09:51  温暖了寂寞  阅读(45)  评论(0编辑  收藏  举报