二分查找一>寻找旋转排序数组中的最小值

2024-10-09 16:03:21 浏览数 (1)

1.题目: 

2.解析:

代码:

代码语言:javascript复制
方法一:以最后一个元素为参照物
        int left = 0, right = nums.length-1;
        int n = nums.length-1;

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

        return nums[left];

        
        //方法二:以第一个元素为参照物
        int left = 0, right = nums.length-1;
        int min = 0;
        while(left < right) {
            int mid = left (right-left)/2;
            if(nums[mid] < nums[0]) right = mid;
            else left = mid 1;
        }
        min = nums[left];

        return min > nums[0] ? nums[0] : min;

0 人点赞