原题链接在这里:
题目:
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.
You may assume no duplicate exists in the array.
题解:
Binary Search, 与类似.
如果nums[mid] < nums[r]说明右边一段是sorted的, minumum 只能出现在包括中点的左边一段.
反之, 说明左边一段是sorted的, minimum只能出现在不包括中点的右边一段.
最后返回nums[r].
Time Compelxity: O(logn). n = nums.length.
Space: O(1).
AC Java:
1 class Solution { 2 public int findMin(int[] nums) { 3 if(nums == null || nums.length == 0){ 4 throw new IllegalArgumentException("Input array is null or empty."); 5 } 6 7 int l = 0; 8 int r = nums.length-1; 9 while(l
跟上, .