File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
solution/1095.Find in Mountain Array Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findInMountainArray (int target , MountainArray mountainArr ) {
3
+ int length = mountainArr .length ();
4
+ int l = 0 , r = length - 1 ;
5
+ while (l < r ) {
6
+ int mid = l + r >>> 1 ;
7
+ if (mountainArr .get (mid ) > mountainArr .get (mid + 1 )) r = mid ;
8
+ else l = mid + 1 ;
9
+ }
10
+ int topIndex = r ;
11
+ int topValue = mountainArr .get (topIndex );
12
+ if (target == topValue ) return topIndex ;
13
+ if (target > topValue ) return -1 ;
14
+ l = 0 ;
15
+ r = topIndex - 1 ;
16
+ while (l < r ) {
17
+ int mid = l + r >>> 1 ;
18
+ if (mountainArr .get (mid ) >= target ) r = mid ;
19
+ else l = mid + 1 ;
20
+ }
21
+ if (mountainArr .get (r ) == target ) {
22
+ return r ;
23
+ }
24
+ l = topIndex + 1 ;
25
+ r = length - 1 ;
26
+ while (l < r ) {
27
+ int mid = l + r >>> 1 ;
28
+ if (mountainArr .get (mid ) <= target ) r = mid ;
29
+ else l = mid + 1 ;
30
+ }
31
+ return mountainArr .get (r ) == target ? r : -1 ;
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments