-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicIndex.java
More file actions
34 lines (29 loc) · 864 Bytes
/
MagicIndex.java
File metadata and controls
34 lines (29 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package leetcode.array;
/**
* @ClassName MagicIndexLcci
* @Description 魔术索引 https://leetcode-cn.com/problems/magic-index-lcci/
* @Author changxuan
* @Date 2020/7/31 下午9:54
**/
public class MagicIndex {
public static void main(String[] args) {
int[] arrays = {0};
System.out.println(findMagicIndex(arrays));
}
public static int findMagicIndex(int[] nums) {
return divideFind(nums, 0, nums.length-1);
}
public static int divideFind(int[] arrays, int low, int high){
if (low > high){
return -1;
}
int mid = (high-low) / 2 + low;
int result = divideFind(arrays, low, mid-1);
if (result != -1){
return result;
}else if (mid == arrays[mid]){
return mid;
}
return divideFind(arrays, mid+1, high);
}
}