Last active
March 15, 2016 20:23
-
-
Save cangoal/12f3b798177332b4b9dd to your computer and use it in GitHub Desktop.
LeetCode - Find Minimum in Rotated Sorted Array
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){ | |
| 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]){ | |
| int m = (l+r)/2; | |
| if(num[m] > num[r]){ | |
| l = m+1; | |
| }else{ | |
| r = m; | |
| } | |
| } | |
| return num[l]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment