【问题标题】:Leetcode BinarySearch Template II Find Minimum in Rotated Sorted ArrayLeetcode BinarySearch 模板 II 在旋转排序数组中查找最小值
【发布时间】:2021-04-14 00:56:42
【问题描述】:

我正在尝试解决二进制搜索模板 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]

谁能指出我的编码逻辑中的缺陷?

编辑:我在我的解决方案中添加了两个案例(对于 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。

【问题讨论】:

  • 一个小技巧,模拟数组只有1个元素的情况。
  • 我认为我的解决方案中存在逻辑问题,我无法弄清楚....在添加 nums.length == 1 或 2 的两种情况后,我仍然收到错误..请查看我在问题中添加的编辑版本。
  • 你可以删除链接,因为你必须登录才能看到资源
  • 删除了链接。
  • @user2755407 我尝试了您的测试用例,但没有收到任何错误输出 2

标签: java binary-search


【解决方案1】:

错误

数组的中间

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

应该是第三个元素所以mid=3

中间值计算错误。
例如,在您的第一次迭代中 left=0right=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]; 
}

【讨论】:

  • 我认为我的解决方案中存在逻辑问题,我无法弄清楚....在添加 nums.length == 1 或 2 的两种情况后,我仍然收到错误..请查看我在问题中添加的编辑版本。
  • 哦,是的,我忘记了输入长度为 2 的情况。首先,我认为我只是认为这是不恰当的,这就是我这样做的原因nums.length &lt; 3
  • 我用长度为 1 2 和 3 的输入尝试了这个解决方案,它可以工作。我会为你做一个小提琴,也许你有错字什么的
  • 尝试 nums = [2, 3, 4, 5, 1]
  • 你必须考虑当你计算中间值时你必须四舍五入,因为当你在第一次迭代中进行整数除法时 5 - 0 / 2 = 2 因为它是 2,5 和小数位会迷路。这是错误的中点,正确的是在第三位
猜你喜欢
  • 1970-01-01
  • 2019-03-31
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 2015-02-25
相关资源
最近更新 更多