Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf480fc

Browse files
find peak element
1 parent8f6730c commitdf480fc

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packagemedium;
2+
3+
publicclassFindPeakElement {
4+
5+
/**
6+
* On discuss, this post has very good explanation about an O(logn) solution:
7+
* https://discuss.leetcode.com/topic/29329/java-solution-and-explanation-using-invariants
8+
*
9+
* Basically, we need to keep this invariant:
10+
* nums[left] > nums[left-1], then we could return left as the result
11+
* or nums[right] > nums[right+1], then we could return right as the result
12+
*/
13+
publicstaticintfindPeakElement_Ologn(int[]nums) {
14+
15+
if(nums ==null ||nums.length ==0)return0;
16+
intleft =0,right =nums.length-1;
17+
while(left+1 <right){
18+
intmid =left + (right-left)/2;
19+
if(nums[mid] <nums[mid+1]){
20+
left =mid;
21+
}else {
22+
right =mid;
23+
}
24+
}
25+
return (left ==nums.length-1 ||nums[left] >nums[left+1]) ?left :right;
26+
27+
}
28+
29+
/**My original O(n) solution.*/
30+
publicstaticintfindPeakElement(int[]nums) {
31+
if(nums ==null ||nums.length ==0)return0;
32+
intn =nums.length,result =0;
33+
for(inti =0;i <n;i++){
34+
if(i ==0 &&n >1 &&nums[i] >nums[i+1]){
35+
result =i;
36+
break;
37+
}elseif(i ==n-1 &&i >0 &&nums[i] >nums[i-1]){
38+
result =i;
39+
break;
40+
}elseif(i >0 &&i <n-1 &&nums[i] >nums[i-1] &&nums[i] >nums[i+1]){
41+
result =i;
42+
break;
43+
}
44+
}
45+
returnresult;
46+
}
47+
48+
publicstaticvoidmain(String...strings){
49+
// int[] nums = new int[]{1,2};
50+
// int[] nums = new int[]{1};
51+
int[]nums =newint[]{1,2,3,1};
52+
// System.out.println(findPeakElement(nums));
53+
System.out.println(findPeakElement_Ologn(nums));
54+
}
55+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Union Find](../../blob/master/MEDIUM/src/medium/NumberOfIslandsUnionFind.java)[DFS](../../blob/master/MEDIUM/src/medium/NumberofIslandsDFS.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS
2626
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[Solution](../../blob/master/EASY/src/easy/RotateArray.java)| O(n)|O(n), could be optimized to O(1) | Easy
2727
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)|[Queue](../../blob/master/MEDIUM/src/medium/BSTIterator_using_q.java)[Stack](../../blob/master/MEDIUM/src/medium/BSTIterator_using_stack.java)| O(1)|O(h)| Medium|
28+
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/)|[Solution](../../blob/master/MEDIUM/src/medium/FindPeakElement.java)| O(1)|O(logn)/O(n)| Binary Search|
2829
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../../blob/master/HARD/src/hard/WordBreakII.java)| ? |O(n^2) | Hard| Backtracking/DFS
2930
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../../blob/master/MEDIUM/src/medium/WordBreak.java)| O(n^2)|O(n) | Medium| DP
3031
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[Solution](../../blob/master/MEDIUM/src/medium/CloneGraph.java)| O(n)|O(n) | Medium| HashMap, BFS

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp