forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path154.c
More file actions
30 lines (25 loc) · 655 Bytes
/
154.c
File metadata and controls
30 lines (25 loc) · 655 Bytes
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
#include <stdio.h>
#include <assert.h>
int findMin(int *nums, int numsSize) {
int l = 0;
int r = numsSize - 1;
while (l < r) {
int m = l + (r - l) / 2;
if (nums[l] > nums[m]) { /* right side is sorted */
r = m;
}
else if (nums[r] < nums[m]) { /* left side is sorted */
l = m + 1;
}
else { /* the sub-array is not rotated, 1 step to deal with dups */
r--;
}
}
return nums[l];
}
int main() {
int nums[] = { 3, 3, 1, 3 };
assert(findMin(nums, sizeof(nums) / sizeof(nums[0])) == 1);
printf("all tests passed!\n");
return 0;
}