-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindPeakElement.java
48 lines (38 loc) · 1.13 KB
/
FindPeakElement.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
public class Solution {
public int findPeakElement(int[] nums) {
int peak=-1;
int [] dummy = new int[nums.length+2];
if(nums.length==0 || nums.length ==1) return 0;
for(int i=0;i<nums.length;i++)
{
if(i==0)
{ if(nums[1]<nums[0]) peak=i;}
if(i==nums.length-1)
{ if(nums[nums.length-2]<nums[nums.length-1]) peak=i;}
if(i!=0 && i!=nums.length-1 && nums[i-1]<nums[i] && nums[i+1]<nums[i])
peak=i;
}
return peak;
}
}
/* public int findPeakElement(int[] num) {
return helper(num,0,num.length-1);
}
public int helper(int[] num,int start,int end){
if(start == end){
return start;
}else if(start+1 == end){
if(num[start] > num[end]) return start;
return end;
}else{
int m = (start+end)/2;
if(num[m] > num[m-1] && num[m] > num[m+1]){
return m;
}else if(num[m-1] > num[m] && num[m] > num[m+1]){
return helper(num,start,m-1);
}else{
return helper(num,m+1,end);
}
}
}
/*