Skip to content

Instantly share code, notes, and snippets.

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