Find Minimum in rotated Sorted ArrayTotal Accepted:
21207 Total Submissions:
65855
Suppose a sorted array is rotated on 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.
Assume no duplicate exists in the array.
Analysis
dichotomy, if Num[mid] and adjacent elements are not ordered, the minimum value is available. Otherwise, see if low and high are ordered and jump to half the order.
[Precautions]
NONE
[CODE]
public class Solution {public int findmin (int[] num) { if (Num==null | | num.length < 1) return 0;//Ask Interv Iewer which value should Return:Integer.MIN_VALUE or throw a Exception. int low = 0, high = num.length-1; while (Low < high) { int mid = low + (high-low)/2; int x = Num[mid]; if (Mid! = num.length-1 && x>num[mid+1]) { return num[mid+1]; } else if (Num[mid] < Num[low]) { h IgH = mid; } else if (Num[mid] > Num[high]) {low = mid; } else { return num[low]; } } return num[low];} }
Leetcode 153:find Minimum in rotated Sorted Array