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

Commit 9a7be0b

Browse files
Count Hills and Valleys in an Array
1 parent dfbc564 commit 9a7be0b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Link: https://leetcode.com/problems/count-hills-and-valleys-in-an-array/
2+
Language: C#
3+
4+
5+
6+
7+
8+
9+
10+
public class Solution {
11+
public int CountHillValley(int[] nums)
12+
{
13+
int counter = 0;
14+
int temp = 0;
15+
List<int> list = new List<int>();
16+
17+
for (int i = 0; i < nums.Length; i++)
18+
{
19+
if (nums[i] != temp)
20+
{
21+
list.Add(nums[i]);
22+
temp = nums[i];
23+
}
24+
}
25+
26+
for (int i = 1; i < list.Count - 1; i++)
27+
{
28+
if ((list[i] > list[i - 1] && list[i] > list[i + 1]) ||
29+
(list[i] < list[i - 1] && list[i] < list[i + 1]))
30+
counter++;
31+
}
32+
33+
return counter;
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /