Always separate the array for three conditions.
Put equal condition individually.
public int findMin(int[] num) {
int l = 0;
int r = num.length - 1;
int min = num[0];
while (l < r) {
int m = (l+r)/2;
if (num[l] < num[m]) {
min = Math.min(num[l], min);
l = m + 1;
} else if (num[l] > num[m]) {
min = Math.min(num[m], min);
r = m - 1;
} else {
min = Math.min(num[m], min);
l++;
}
}
min = Math.min(num[r], min);
min = Math.min(num[l], min);
return min;
}
No comments:
Post a Comment