Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
Find the minimum element.
The array may contain duplicates.
解题思路:
参考Java for LeetCode 081 Search in Rotated Sorted Array II JAVA实现如下:
public int findMin(int[] nums) { int left = 0, right = nums.length - 1, res = nums[0]; while (left <= right) { res = Math.min(nums[left],Math.min(res, nums[(right + left) / 2])); if (nums[(right + left) / 2] < nums[left]) right = (right + left) / 2 - 1; else if (nums[(right + left) / 2] > nums[left]) left = (right + left) / 2 + 1; else left++; } return res; }