You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
600 B
32 lines
600 B
package class01_01;
|
|
|
|
public class Code06_BSAwesome {
|
|
|
|
public static int getLessIndex(int[] arr) {
|
|
if (arr == null || arr.length == 0) {
|
|
return -1; // no exist
|
|
}
|
|
if (arr.length == 1 || arr[0] < arr[1]) {
|
|
return 0;
|
|
}
|
|
if (arr[arr.length - 1] < arr[arr.length - 2]) {
|
|
return arr.length - 1;
|
|
}
|
|
int left = 1;
|
|
int right = arr.length - 2;
|
|
int mid = 0;
|
|
while (left < right) {
|
|
mid = (left + right) / 2;
|
|
if (arr[mid] > arr[mid - 1]) {
|
|
right = mid - 1;
|
|
} else if (arr[mid] > arr[mid + 1]) {
|
|
left = mid + 1;
|
|
} else {
|
|
return mid;
|
|
}
|
|
}
|
|
return left;
|
|
}
|
|
|
|
}
|