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 64683d7

Browse files
committed
"Find Minimum in Rotated Sorted Array"
1 parent 7227f89 commit 64683d7

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
@@ -155,7 +155,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
155155
| 156 | [Binary Tree Upside Down]| |
156156
| 155 | [Min Stack] | [C](src/155.c) |
157157
| 154 | [Find Minimum in Rotated Sorted Array II] | |
158-
| 153 | [Find Minimum in Rotated Sorted Array] | |
158+
| 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) |
161161
| 150 | [Evaluate Reverse Polish Notation] | [C](src/150.c) |

‎src/153.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 */
16+
r = m;
17+
}
18+
}
19+
return nums[l];
20+
}
21+
22+
int main() {
23+
int nums[] = { 3, 1, 2 };
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 によって変換されたページ (->オリジナル) /