leetcode二进制搜索模板ii在旋转排序数组中查找最小值

0g0grzrc  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(343)

我正在尝试解决模板2中的二进制搜索问题..在旋转排序数组中查找最小值。问题如下:
假设按升序排序的长度为n的数组在1到n次之间旋转。例如,数组nums=[0,1,2,4,5,6,7]可能会变成:

[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.

请注意,将数组[a[0]、a[1]、a[2]、…、a[n-1]]旋转1次会导致数组[a[n-1]、a[0]、a[1]、a[2]、…、a[n-2]]。
给定已排序的旋转数组nums,返回此数组的最小元素。
例1:

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.

例2:

Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.

例3:

Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.

约束条件:

n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
All the integers of nums are unique.
nums is sorted and rotated between 1 and n times.

我的解决方案代码如下:

public int findMin(int[] nums) {

        if(nums == null || nums.length == 0) {
            return -1;
        }

        if(nums.length == 1) return nums[0];

        if(nums.length == 2) {

            return (nums[0] > nums[1])? nums[1]:nums[0]; 

        }

        int left = 0;
        int right = nums.length;

        while(left < right) {

            int mid = left + (right - left)/2;

            // [3,4,5,1,2]
            if(nums[mid] >  nums[mid + 1]) {

                return nums[mid + 1];

            } else if(nums[mid] <  nums[mid + 1]) {

                right = mid;

            }

        }

        if(left != nums.length) {

            return nums[left];

        }

        return -1;
}

我的代码适用于以下示例集:

nums = [3,4,5,1,2]
nums = [4,5,6,7,0,1,2]
nums = [11,13,15,17]
nuts = [4,5,6,7,0,1,2]

但是当我试图提交代码时,我得到的错误如下:

Runtime Error Message:
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
  at line 16, Solution.findMin
  at line 54, __DriverSolution__.__helper__
  at line 84, __Driver__.main
Last executed input:
[1]

有人能指出我的编码逻辑中的缺陷吗?
编辑:我在解决方案中添加了两个case(对于nums.length==1或2)。

if(nums.length == 1) return nums[0];

if(nums.length == 2) {

    return (nums[0] > nums[1])? nums[1]:nums[0]; 

}

但是当nums=[2,3,4,5,1]的时候,我还是得到了一个错误,也就是说,我得到的结果值是2,但是原来的答案是1。

kqqjbcuj

kqqjbcuj1#

臭虫

你阵型的中部

int[] mid = {1, 2, 3, 4, 5}

应该是第三个元素 mid=3 中间值计算错误。
例如,在您的第一次迭代中 left=0 以及 right=5 所以下面的计算结果是

int mid = left + (right - left)/2
// result -> mid=2

原因
因为将结果赋给整数,所以小数部分将丢失。结果不是2,5,2

汇总

下面的函数将对结果进行四舍五入,以便计算正确的中值

public static long roundUp(long num, long divisor) {
    return (num + divisor - 1) / divisor;
}

使用调用函数

int mid = (int)YourClass.roundUp((left + (right - left)), 2);

其他案例

你必须考虑长度为1,长度为2的情况

if(nums == null || nums.length < 1) {
            return -1;
} else if(nums.length == 1){
  // you don't have to apply a binary search there is only one element
  // you can directly return it
  return nums[0];
} else if(nums.length == 2){
  return nums[0] < nums[1]? nums[0] : nums[1]; 
}

相关问题