Skip to content

Instantly share code, notes, and snippets.

@cangoal
Last active March 15, 2016 20:23
Show Gist options
  • Save cangoal/12f3b798177332b4b9dd to your computer and use it in GitHub Desktop.
Save cangoal/12f3b798177332b4b9dd to your computer and use it in GitHub Desktop.
LeetCode - Find Minimum in Rotated Sorted Array
//
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