[leetcode]154. Find Minimum in Rotated Sorted Array II@Java解题报告

来源:互联网 发布:anywhere2 mac 蓝牙 编辑:程序博客网 时间:2024/06/11 14:39

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/


Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Suppose an array sorted in ascending order 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.





package go.jacob.day815;/** * 154. Find Minimum in Rotated Sorted Array II *  * @author Jacob * */public class Demo3 {public int findMin(int[] nums) {int left = 0, right = nums.length - 1;while (left < right) {int mid = left + (right - left) / 2;//如果nums[left] > nums[mid],说明存在left,mid降序if (nums[left] > nums[mid]){left ++;right=mid;}//如果nums[mid] > nums[right],说明存在mid,right降序else if (nums[mid] > nums[right]) {left=mid+1;} else//不存在降序,说明有重复元素,把数组长度缩短1;right--;}return nums[left];}}


阅读全文
0 0
原创粉丝点击