Skip to content

Commit c215602

Browse files
authored
Create 0162-find-peak-element.js
1 parent 39611ac commit c215602

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

javascript/0162-find-peak-element.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findPeakElement = function(nums) {
6+
let [l, r] = [0, nums.length - 1];
7+
let mid = null;
8+
while (l <= r){
9+
mid = (l + r) >> 1;
10+
if (mid < nums.length - 1 && nums[mid] < nums[mid+1]){
11+
l = mid + 1;
12+
}
13+
else if (mid > 0 && nums[mid] < nums[mid-1]) {
14+
r = mid - 1;
15+
}
16+
else {
17+
break;
18+
}
19+
}
20+
return mid;
21+
};

0 commit comments

Comments
 (0)