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 d163551

Browse files
committed
"Find Minimum in Rotated Sorted Array II"
1 parent 64683d7 commit d163551

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
154154
| 157 | [Read N Characters Given Read4]| |
155155
| 156 | [Binary Tree Upside Down]| |
156156
| 155 | [Min Stack] | [C](src/155.c) |
157-
| 154 | [Find Minimum in Rotated Sorted Array II] | |
157+
| 154 | [Find Minimum in Rotated Sorted Array II] | [C](src/154.c) |
158158
| 153 | [Find Minimum in Rotated Sorted Array] | [C](src/153.c) |
159159
| 152 | [Maximum Product Subarray] | [C](src/152.c) |
160160
| 151 | [Reverse Words in a String] | [C](src/151.c) |

‎src/154.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <assert.h>
3+
4+
int findMin(int *nums, int numsSize) {
5+
int l = 0;
6+
int r = numsSize - 1;
7+
while (l < r) {
8+
int m = l + (r - l) / 2;
9+
if (nums[l] > nums[m]) { /* right side is sorted */
10+
r = m;
11+
}
12+
else if (nums[r] < nums[m]) { /* left side is sorted */
13+
l = m + 1;
14+
}
15+
else { /* the sub-array is not rotated, 1 step to deal with dups */
16+
r--;
17+
}
18+
}
19+
return nums[l];
20+
}
21+
22+
int main() {
23+
int nums[] = { 3, 3, 1, 3 };
24+
25+
assert(findMin(nums, sizeof(nums) / sizeof(nums[0])) == 1);
26+
27+
printf("all tests passed!\n");
28+
29+
return 0;
30+
}

0 commit comments

Comments
(0)

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