-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode852.java
More file actions
35 lines (32 loc) · 1003 Bytes
/
LeetCode852.java
File metadata and controls
35 lines (32 loc) · 1003 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
35
public class LeetCode852 {
public static void main(String[] args) {
// 输入:arr = [0,1,0]
// 输出:1
System.out.println(new Solution852().peakIndexInMountainArray(new int[] { 0,
1, 0 }));
// 输入:arr = [0,2,1,0]
// 输出:1
System.out.println(new Solution852().peakIndexInMountainArray(new int[] { 0,
2, 1, 0 }));
// 输入:arr = [0,10,5,2]
// 输出:1
System.out.println(new Solution852().peakIndexInMountainArray(new int[] { 0,
10, 5, 2 }));
}
}
class Solution852 {
public int peakIndexInMountainArray(int[] arr) {
int left = 1;
int right = arr.length - 2;
int mid;
while (left <= right) {
mid = left + (right - left) / 2;
if (arr[mid] <= arr[mid + 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
}