Skip to content

Commit

Permalink
Initial commit for Leetcode algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
danghai committed Aug 8, 2019
1 parent 95e9c97 commit 593d76a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# C
C
========

## LeetCode Algorithm
- Solution for [LeetCode](https://leetcode.com/problemset/all/)

## Computer Oriented Statistical Methods
- Gauss_Elimination
Expand All @@ -21,13 +25,13 @@
- stack
- queue
- dictionary
linked_list
- linked_list
- singly_link_list_deletion
- stack_using_linkedlists
binary_trees
- binary_trees
- create_node
- recursive_traversals
trie
- trie
- trie


Expand All @@ -48,7 +52,7 @@
- SelectionSort
- ShakerSort
- HeapSort

## Hashing
- sdbm
- djb2
Expand Down
11 changes: 11 additions & 0 deletions leetcode/README.md
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|
13 changes: 13 additions & 0 deletions leetcode/src/35.c
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;
}
27 changes: 27 additions & 0 deletions leetcode/src/704.c
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;
}

29 changes: 29 additions & 0 deletions leetcode/src/905.c
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;
}

0 comments on commit 593d76a

Please sign in to comment.