forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit for Leetcode algorithm
- Loading branch information
Showing
5 changed files
with
89 additions
and
5 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
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,11 @@ | ||
LeetCode | ||
======== | ||
|
||
### LeetCode Algorithm | ||
|
||
|
||
| # | Title | Solution | Difficulty | | ||
|---| ----- | -------- | ---------- | | ||
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [C](./leetcode/src/35.c)|Easy| | ||
|704|[Search Insert Position](https://leetcode.com/problems/binary-search/) | [C](./leetcode/src/704.c)|Easy| | ||
|905|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [C](./leetcode/src/905.c)|Easy| |
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,13 @@ | ||
int searchInsert(int* nums, int numsSize, int target){ | ||
int low = 0, high = numsSize - 1, mid; | ||
while (low <= high) { | ||
mid = low + (high - low) / 2; | ||
if (target > nums[mid]) | ||
low = mid + 1; | ||
else if (target < nums[mid]) | ||
high = mid - 1; | ||
else | ||
return mid; | ||
} | ||
return low; | ||
} |
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,27 @@ | ||
int search(int* nums, int numsSize, int target){ | ||
int low = 0, high = numsSize - 1; | ||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
if (target > nums[mid]) | ||
low = mid + 1; | ||
else if (target < nums[mid]) | ||
high = mid - 1; | ||
else | ||
return mid; | ||
} | ||
return -1; | ||
} | ||
|
||
/* Another solution: Using bsearch() */ | ||
int cmpint (const void *a, const void *b) { | ||
return *(int *) a - *(int *)b; | ||
} | ||
|
||
int search(int* nums, int numsSize, int target){ | ||
int *ret = bsearch(&target, nums, numsSize, sizeof(int), cmpint); | ||
if (ret) | ||
return (ret - nums); | ||
else | ||
return -1; | ||
} | ||
|
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,29 @@ | ||
/** | ||
* 905. Sort Array By Parity | ||
* Given an array A of non-negative integers, return an array consisting of | ||
* all the even elements of A, followed by all the odd elements of A. | ||
* You may return any answer array that satisfies this condition. | ||
* Example 1: | ||
* Input: [3,1,2,4] | ||
* Output: [2,4,3,1] | ||
* The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. | ||
* | ||
* Note: The returned array must be malloced, assume caller calls free(). | ||
*/ | ||
int* sortArrayByParity(int* A, int ASize, int* returnSize){ | ||
int *retArr = malloc(ASize * sizeof(int)); | ||
int oddIndex = ASize - 1; | ||
int evenIndex = 0; | ||
*returnSize = ASize; | ||
for (int i = 0; i < ASize; i++) { | ||
if(A[i] % 2 == 0) { | ||
retArr[evenIndex] = A[i]; | ||
evenIndex++; | ||
} else { | ||
retArr[oddIndex] = A[i]; | ||
oddIndex--; | ||
} | ||
} | ||
|
||
return retArr; | ||
} |