Skip to content

Commit f429ac0

Browse files
Refine
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 37f2e43 commit f429ac0

File tree

2 files changed

+45
-87
lines changed

2 files changed

+45
-87
lines changed

033_search_in_rotated_sorted_array/rotated_array.c

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,33 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
static int binary_search(int *nums, int size, int target)
5-
{
6-
int low = -1;
7-
int high = size;
8-
while (low + 1 < high) {
9-
int mid = low + (high - low) / 2;
10-
if (target > nums[mid]) {
11-
low = mid;
12-
} else {
13-
high = mid;
14-
}
15-
}
16-
if (high == size || nums[high] != target) {
17-
return -1;
18-
} else {
19-
return high;
20-
}
21-
}
224

23-
static int start_find(int *nums, int size)
5+
static int search(int* nums, int numsSize, int target)
246
{
25-
int low = 0;
26-
int high = size - 1;
27-
while (low < high && nums[low] > nums[high]) {
28-
int mid = low + (high - low) / 2;
29-
if (nums[mid] > nums[high]) {
30-
low = mid + 1;
31-
} else if (nums[mid] < nums[low]) {
32-
/* Assume no duplicate exists in arry */
33-
high = mid;
7+
int lo = 0;
8+
int hi = numsSize - 1;
9+
while (lo <= hi) {
10+
int mid = lo + (hi - lo) / 2;
11+
if (nums[mid] == target) {
12+
return mid;
3413
}
35-
}
36-
return low;
37-
}
3814

39-
static int search(int* nums, int numsSize, int target)
40-
{
41-
if (numsSize <= 0) {
42-
return -1;
43-
}
44-
if (numsSize == 1) {
45-
return target == nums[0] ? 0 : -1;
15+
if (nums[lo] <= nums[mid]) {
16+
if (nums[lo] <= target && target < nums[mid]) {
17+
hi = mid - 1;
18+
} else {
19+
lo = mid + 1;
20+
}
21+
} else {
22+
if (nums[mid] < target && target <= nums[hi]) {
23+
lo = mid + 1;
24+
} else {
25+
hi = mid - 1;
26+
}
27+
}
4628
}
4729

48-
int i = start_find(nums, numsSize);
49-
if (i == 0) {
50-
return binary_search(nums, numsSize, target);
51-
} else if (target >= nums[0]) {
52-
return binary_search(nums, i, target);
53-
} else if (target <= nums[numsSize - 1]) {
54-
int index = binary_search(nums + i, numsSize - i, target);
55-
return index >= 0 ? index + i : -1;
56-
} else {
57-
return -1;
58-
}
30+
return -1;
5931
}
6032

6133
int main(int argc, char **argv)

081_search_in_rotated_sorted_array_ii/search_rotated_array.c

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,37 @@
22
#include <stdlib.h>
33
#include <stdbool.h>
44

5-
static int binary_search(int *nums, int size, int target)
6-
{
7-
int low = -1;
8-
int high = size;
9-
while (low + 1 < high) {
10-
int mid = low + (high - low) / 2;
11-
if (target > nums[mid]) {
12-
low = mid;
13-
} else {
14-
high = mid;
15-
}
16-
}
17-
if (high == size || nums[high] != target) {
18-
return -1;
19-
} else {
20-
return high;
21-
}
22-
}
235

246
static bool search(int* nums, int numsSize, int target)
257
{
26-
if (numsSize <= 0) {
27-
return false;
28-
}
29-
30-
if (numsSize == 1) {
31-
return target == nums[0];
32-
}
8+
int lo = 0;
9+
int hi = numsSize - 1;
10+
while (lo <= hi) {
11+
int mid = lo + (hi - lo) / 2;
12+
if (nums[mid] == target) {
13+
return true;
14+
}
3315

34-
int i;
35-
for (i = 1; i < numsSize; i++) {
36-
if (nums[i] < nums[i - 1]) {
37-
break;
16+
if (nums[lo] == nums[mid] && nums[mid] == nums[hi]) {
17+
/* Not sure which side contains the peak value, reduce search range */
18+
lo++;
19+
hi--;
20+
} else if (nums[lo] <= nums[mid]) { /* lo might be equal to mid */
21+
if (nums[lo] <= target && target < nums[mid]) {
22+
hi = mid - 1;
23+
} else {
24+
lo = mid + 1;
25+
}
26+
} else {
27+
if (nums[mid] < target && target <= nums[hi]) {
28+
lo = mid + 1;
29+
} else {
30+
hi = mid - 1;
31+
}
3832
}
3933
}
4034

41-
if (i == 0) {
42-
return binary_search(nums, numsSize, target) >= 0;
43-
} else if (target >= nums[0]) {
44-
return binary_search(nums, i, target) >= 0;
45-
} else if (target <= nums[numsSize - 1]) {
46-
return binary_search(nums + i, numsSize - i, target) >= 0;
47-
} else {
48-
return -1;
49-
}
35+
return false;
5036
}
5137

5238
int main(int argc, char **argv)

0 commit comments

Comments
 (0)