Skip to content

Commit acc26fa

Browse files
Update 0162-find-peak-element.java
The java code for this problem is missing. So I added my code which passed the testcases on leetcode. submission link: https://leetcode.com/problems/find-peak-element/submissions/1135183935/
1 parent 41dfdb7 commit acc26fa

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

java/0162-find-peak-element.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int findPeakElement(int[] nums) {
3+
int l = 0, r = nums.length-1;
4+
while(l<=r){
5+
int m = l + (r-l)/2;
6+
int left = (m-1==-1)?Integer.MIN_VALUE:nums[m-1];
7+
int right = (m+1==nums.length)?Integer.MIN_VALUE:nums[m+1];
8+
if(nums[m]>left && nums[m]>right) return m;
9+
else if(nums[m]<left) r=m-1;
10+
else l = m+1;
11+
}
12+
return 0;
13+
}
14+
}

0 commit comments

Comments
 (0)