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

Commit110836f

Browse files
add 852
1 parent9b985cb commit110836f

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Your ideas/fixes/algorithms are more than welcome!
3434
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java)| O(n)| O(1)||Easy|
3535
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java)| O(n)| O(1)||Easy|
3636
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java)| O(n)| O(k)||Easy|
37+
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java)| O(n)| O(1)||Easy|
3738
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java)| O(n)| O(1)||Easy|
3839
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java)| O(n)| O(1)||Easy|
3940
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java)| O(n)| O(n)||Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**852. Peak Index in a Mountain Array
4+
*
5+
* Let's call an array A a mountain if the following properties hold:
6+
*
7+
* A.length >= 3
8+
* There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
9+
* Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
10+
*
11+
* Example 1:
12+
*
13+
* Input: [0,1,0]
14+
* Output: 1
15+
* Example 2:
16+
*
17+
* Input: [0,2,1,0]
18+
* Output: 1
19+
* Note:
20+
*
21+
* 3 <= A.length <= 10000
22+
* 0 <= A[i] <= 10^6
23+
* A is a mountain, as defined above.*/
24+
publicclass_852 {
25+
publicstaticclassSolution1 {
26+
publicintpeakIndexInMountainArray(int[]A) {
27+
for (inti =1;i <A.length -1;i++) {
28+
if (A[i] >A[i+1]) {
29+
returni;
30+
}
31+
}
32+
return0;
33+
}
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._852;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.TestCase.assertEquals;
8+
9+
publicclass_852Test {
10+
privatestatic_852.Solution1solution1;
11+
privatestaticint[]A;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_852.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
A =newint[]{0,1,0};
21+
assertEquals(1,solution1.peakIndexInMountainArray(A));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
A =newint[]{0,2,1,0};
27+
assertEquals(1,solution1.peakIndexInMountainArray(A));
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp