File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun findInMountainArray (target : Int , mountainArr : MountainArray ): Int {
3+ var l = 1
4+ var r = mountainArr.length() - 2
5+ var peak = - 1
6+ while (l <= r) {
7+ val m = (l + r) / 2
8+ val left = mountainArr.get(m - 1 )
9+ val mid = mountainArr.get(m)
10+ val right = mountainArr.get(m + 1 )
11+
12+ if (left < mid && mid < right) {
13+ l = m + 1
14+ } else if (left > mid && mid > right) {
15+ r = m - 1
16+ } else {
17+ peak = m
18+ break
19+ }
20+ }
21+
22+ l = 0
23+ r = peak
24+ while (l <= r) {
25+ val m = (l + r) / 2
26+ val value = mountainArr.get(m)
27+ if (value < target)
28+ l = m + 1
29+ else if (value > target)
30+ r = m - 1
31+ else
32+ return m
33+ }
34+
35+ l = peak
36+ r = mountainArr.length() - 1
37+ while (l <= r) {
38+ val m = (l + r) / 2
39+ val value = mountainArr.get(m)
40+ if (value < target)
41+ r = m - 1
42+ else if (value > target)
43+ l = m + 1
44+ else
45+ return m
46+ }
47+
48+ return - 1
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments