【发布时间】: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