Skip to content

Commit c69b910

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <[email protected]>
1 parent a502841 commit c69b910

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

0162_find_peak_element/peak.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
static int findPeakElement(int* nums, int numsSize)
5-
{
6-
if (numsSize == 1) {
7-
return nums[0];
8-
}
94

5+
int findPeakElement(int* nums, int numsSize)
6+
{
107
int i;
11-
for (i = 1; i < numsSize && nums[i] > nums[i - 1]; i++) {}
8+
for (i = 1; i < numsSize; i++) {
9+
if (nums[i] < nums[i - 1]) {
10+
break;
11+
}
12+
}
1213
return i - 1;
1314
}
1415

0162_find_peak_element/peak.cc

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int findPeakElement(vector<int>& nums) {
8+
int i;
9+
for (i = 1; i < nums.size(); i++) {
10+
if (nums[i] < nums[i - 1]) {
11+
break;
12+
}
13+
}
14+
return i - 1;
15+
}
16+
};

0 commit comments

Comments
 (0)