forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
3f10471
commit bf6b1a5
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test rotated_array.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int binary_search(int *nums, int size, int target) | ||
{ | ||
int low = -1; | ||
int high = size; | ||
while (low + 1 < high) { | ||
int mid = low + (high - low) / 2; | ||
if (target > nums[mid]) { | ||
low = mid; | ||
} else { | ||
high = mid; | ||
} | ||
} | ||
if (high == size || nums[high] != target) { | ||
return -1; | ||
} else { | ||
return high; | ||
} | ||
} | ||
|
||
static int start_find(int *nums, int size) | ||
{ | ||
int low = 0; | ||
int high = size - 1; | ||
while (low < high && nums[low] > nums[high]) { | ||
int mid = low + (high - low) / 2; | ||
if (nums[mid] > nums[high]) { | ||
low = mid + 1; | ||
} else if (nums[mid] < nums[low]) { | ||
/* Assume no duplicate exists in arry */ | ||
high = mid; | ||
} | ||
} | ||
return low; | ||
} | ||
|
||
static int search(int* nums, int numsSize, int target) { | ||
if (numsSize <= 0) { | ||
return -1; | ||
} | ||
if (numsSize == 1) { | ||
return target == nums[0] ? 0 : -1; | ||
} | ||
|
||
int i = start_find(nums, numsSize); | ||
if (i == 0) { | ||
return binary_search(nums, numsSize, target); | ||
} else if (target >= nums[0]) { | ||
return binary_search(nums, i, target); | ||
} else if (target <= nums[numsSize - 1]) { | ||
int index = binary_search(nums + i, numsSize - i, target); | ||
return index >= 0 ? index + i : -1; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i; | ||
int target = atoi(argv[1]); | ||
int size = argc - 2; | ||
int *nums = malloc(size * sizeof(int)); | ||
|
||
for (i = 0; i < argc - 2; i++) { | ||
nums[i] = atoi(argv[i + 2]); | ||
} | ||
printf("%d\n", search(nums, size, target)); | ||
return 0; | ||
} |