Last active
March 15, 2016 20:27
-
-
Save cangoal/27bf858aabf301389202 to your computer and use it in GitHub Desktop.
LeetCode - Find Minimum in Rotated Sorted Array II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| public int findMin(int[] nums) { | |
| if(nums == null || nums.length==0) return -1; | |
| int left = 0, right = nums.length - 1; | |
| while(left <= right){ | |
| while(left < right && nums[left] == nums[right]) left++; | |
| if(nums[left] <= nums[right]) return nums[left]; | |
| int mid = (left + right) / 2; | |
| if(nums[mid] <= nums[right]){ | |
| right = mid; | |
| } else { | |
| left = mid + 1; | |
| } | |
| } | |
| return nums[right]; | |
| } | |
| // | |
| public int findMin(int[] num) { | |
| int L = 0, R = num.length - 1; | |
| while (L < R && num[L] >= num[R]) { | |
| // while(L<R && num[L]==num[R]){ | |
| // R--; | |
| // } | |
| int M = (L + R) / 2; | |
| if (num[M] > num[R]) { | |
| L = M + 1; | |
| } else if(num[M] < num[R]){ | |
| R = M; | |
| }else{ //num[L]==num[M]==num[R] | |
| L++; | |
| } | |
| } | |
| return num[L]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment