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;
	}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。