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

Commit1a5bc70

Browse files
author
applewjg
committed
Find Peek Element
Change-Id: I1689700f2e30a5f062739d4f48020b9d5d52dac8
1 parentbb11983 commit1a5bc70

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎FindPeakElement.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 06, 2014
4+
Problem: Find Peak Element
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/find-peak-element/
7+
Notes:
8+
A peak element is an element that is greater than its neighbors.
9+
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
10+
You may imagine that num[-1] = num[n] = -∞.
11+
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
12+
13+
Find the peak element.
14+
*/
15+
publicclassSolution {
16+
publicintfindPeakElement(int[]num) {
17+
intleft =0,right =num.length -1,mid = -1;
18+
while (left <=right) {
19+
mid = (left +right) /2;
20+
if ((mid ==0 ||num[mid-1] <=num[mid]) && (mid ==num.length -1 ||num[mid] >=num[mid+1]))
21+
returnmid;
22+
if (mid >0 &&num[mid-1] >num[mid]) {
23+
right =mid -1;
24+
}elseif (num[mid+1] >num[mid]) {
25+
left =mid +1;
26+
}
27+
}
28+
returnmid;
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp