forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
162.c
31 lines (28 loc) · 858 Bytes
/
162.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <assert.h>
int findPeakElement(int* nums, int numsSize) {
int left = 0, right = numsSize - 1, mid;
while (left < right) {
mid = left + (right - left) / 2;
if ((mid == 0 && nums[mid] > nums[mid + 1])
|| (mid == numsSize - 1 && nums[mid] > nums[mid - 1])
|| (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]))
{
return mid;
}
else if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
}
else if (nums[mid] < nums[mid - 1]) {
right = mid - 1;
}
}
return left;
}
int main() {
int nums0[] = { 1, 2, 3, 1 };
int nums0[] = { 1, 2 };
assert(findPeakElement(sizeof(nums0) / sizeof(nums0[0])) == 2);
assert(findPeakElement(sizeof(nums1) / sizeof(nums1[0])) == 1);
return 0;
}