mirror of https://github.com/TheAlgorithms/C
Update more leetcode solution
This commit is contained in:
parent
b23cbb387d
commit
30bfcef198
|
@ -8,38 +8,56 @@ LeetCode
|
|||
|---| ----- | -------- | ---------- |
|
||||
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium|
|
||||
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy|
|
||||
|27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy|
|
||||
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium|
|
||||
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy|
|
||||
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [C](./src/35.c)|Easy|
|
||||
|53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [C](./src/53.c)|Easy|
|
||||
|82|[Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [C](./src/82.c)|Medium|
|
||||
|83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [C](./src/83.c)|Easy|
|
||||
|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [C](./src/94.c)|Medium|
|
||||
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [C](./src/101.c)|Easy|
|
||||
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [C](./src/104.c)|Easy|
|
||||
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [C](./src/108.c)|Easy|
|
||||
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [C](./src/109.c)|Medium|
|
||||
|112|[Path Sum](https://leetcode.com/problems/path-sum/) | [C](./src/112.c)|Easy|
|
||||
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [C](./src/110.c)|Easy|
|
||||
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C](./src/125.c)|Easy|
|
||||
|136|[Single Number](https://leetcode.com/problems/single-number/) | [C](./src/136.c)|Easy|
|
||||
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [C](./src/141.c)|Easy|
|
||||
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [C](./src/142.c)|Medium|
|
||||
|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [C](./src/153.c)|Medium|
|
||||
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [C](./src/160.c)|Easy|
|
||||
|169|[Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c)|Easy|
|
||||
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c)|Easy|
|
||||
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c)|Easy|
|
||||
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium|
|
||||
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [C](./src/217.c)|Easy|
|
||||
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [C](./src/226.c)|Easy|
|
||||
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | [C](./src/234.c)|Easy|
|
||||
|268|[Missing Number](https://leetcode.com/problems/missing-number/) | [C](./src/268.c)|Easy|
|
||||
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/) | [C](./src/278.c)|Easy|
|
||||
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C](./src/283.c)|Easy|
|
||||
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C](./src/287.c)|Medium|
|
||||
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) | [C](./src/344.c)|Easy|
|
||||
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C](./src/387.c)|Easy|
|
||||
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C](./src/389.c)|Easy|
|
||||
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C](./src/404.c)|Easy|
|
||||
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [C](./src/442.c)|Medium|
|
||||
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [C](./src/509.c)|Easy|
|
||||
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c)|Easy|
|
||||
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy|
|
||||
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C](./src/617.c)|Easy|
|
||||
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C](./src/674.c)|Easy|
|
||||
|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [C](./src/700.c)|Easy|
|
||||
|701|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [C](./src/701.c)|Medium|
|
||||
|704|[Binary Search](https://leetcode.com/problems/binary-search/) | [C](./src/704.c)|Easy|
|
||||
|709|[To Lower Case](https://leetcode.com/problems/to-lower-case/) | [C](./src/709.c)|Easy|
|
||||
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/) | [C](./src/771.c)|Easy|
|
||||
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [C](./src/852.c)|Easy|
|
||||
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [C](./src/876.c)|Easy|
|
||||
|905|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [C](./src/905.c)|Easy|
|
||||
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/) | [C](./src/917.c)|Easy|
|
||||
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [C](./src/938.c)|Easy|
|
||||
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [C](./src/965.c)|Easy|
|
||||
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c)|Easy|
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
struct TreeNode* buildBST(struct ListNode* head, struct ListNode* tail) {
|
||||
if(head == tail)
|
||||
return NULL;
|
||||
struct ListNode* slow = head, *fast = head;
|
||||
while(fast != tail && fast->next != tail) {
|
||||
fast = fast->next->next;
|
||||
slow = slow->next;
|
||||
}
|
||||
struct TreeNode* node = malloc(sizeof(struct TreeNode));
|
||||
node->val = slow->val;
|
||||
node->left = buildBST(head, slow);
|
||||
node->right = buildBST(slow->next, tail);
|
||||
return node;
|
||||
}
|
||||
struct TreeNode* sortedListToBST(struct ListNode* head){
|
||||
if (!head)
|
||||
return NULL;
|
||||
else
|
||||
return buildBST(head, NULL);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
bool hasPathSum(struct TreeNode* root, int sum) {
|
||||
if (root == NULL)
|
||||
return 0;
|
||||
if (!root->left && !root->right && sum - root->val == 0)
|
||||
return 1;
|
||||
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
struct ListNode *detectCycle(struct ListNode *head) {
|
||||
if (head == NULL || head->next == NULL)
|
||||
return NULL;
|
||||
struct ListNode *slow, *fast;
|
||||
slow = fast = head;
|
||||
while(fast && fast->next) {
|
||||
slow = slow->next;
|
||||
fast = fast->next->next;
|
||||
if(slow == fast) {
|
||||
struct ListNode *entry = head;
|
||||
while(slow != entry) {
|
||||
slow = slow -> next;
|
||||
entry = entry -> next;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
int findMin(int* nums, int numsSize){
|
||||
int low = 0, high = numsSize - 1;
|
||||
while (low < high) {
|
||||
int mid = low + (high - low) / 2;
|
||||
/* minimum is on left side */
|
||||
if (nums[mid] < nums[high])
|
||||
high = mid;
|
||||
/* minimum is on right side */
|
||||
else
|
||||
low = mid + 1;
|
||||
}
|
||||
return nums[low];
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
|
||||
struct ListNode *cur1 = headA, *cur2 = headB;
|
||||
if(cur1 == NULL || cur2 == NULL)
|
||||
return NULL;
|
||||
while (cur1 && cur2 && cur1 != cur2) {
|
||||
cur1 = cur1 -> next;
|
||||
cur2 = cur2 -> next;
|
||||
if (cur1 == cur2)
|
||||
return cur1;
|
||||
if(!cur1)
|
||||
cur1 = headB;
|
||||
if(!cur2)
|
||||
cur2 = headA;
|
||||
}
|
||||
return cur1;
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
struct ListNode* removeElements(struct ListNode* head, int val){
|
||||
if (head == NULL)
|
||||
return NULL;
|
||||
if(head->val == val) {
|
||||
return removeElements(head->next, val);
|
||||
} else {
|
||||
head -> next = removeElements(head->next, val);
|
||||
}
|
||||
return head;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
struct ListNode* swapPairs(struct ListNode* head) {
|
||||
if(!head || !head->next)
|
||||
return head;
|
||||
struct ListNode *tmp = head->next;
|
||||
head->next = swapPairs(head->next->next);
|
||||
tmp -> next = head;
|
||||
return tmp;
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
int removeElement(int* nums, int numsSize, int val) {
|
||||
int i, start = 0;
|
||||
for (i = 0; i < numsSize; i++) {
|
||||
if(nums[i] != val)
|
||||
nums[start++] = nums[i];
|
||||
}
|
||||
return start;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
// Forward declaration of isBadVersion API.
|
||||
bool isBadVersion(int version);
|
||||
|
||||
int firstBadVersion(int n) {
|
||||
int low = 1, high = n;
|
||||
while (low <= high) {
|
||||
int mid = low + (high - low) / 2;
|
||||
if(isBadVersion(mid)) {
|
||||
high = mid - 1;
|
||||
} else {
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
return low;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
void moveZeroes(int* nums, int numsSize){
|
||||
int i, start = 0;
|
||||
for (i = 0; i < numsSize; i++) {
|
||||
if(nums[i])
|
||||
nums[start++] = nums[i];
|
||||
}
|
||||
for(;start < numsSize; start++) {
|
||||
nums[start] = 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
int fib(int N){
|
||||
if(N == 0)
|
||||
return 0;
|
||||
if(N == 1)
|
||||
return 1;
|
||||
return fib(N - 1) + fib(N - 2);
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
int maxcmp(int a, int b) {
|
||||
return a >= b ? a : b;
|
||||
}
|
||||
|
||||
int maxSubArray(int* nums, int numsSize){
|
||||
int maxSoFar = nums[0], maxEndingHere = nums[0];
|
||||
for(int i = 1; i < numsSize; i++) {
|
||||
maxEndingHere = maxcmp(maxEndingHere + nums[i], nums[i]);
|
||||
maxSoFar = maxcmp(maxSoFar, maxEndingHere);
|
||||
}
|
||||
return maxSoFar;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
int findLengthOfLCIS(int* nums, int numsSize){
|
||||
int maxval = 1, i, count = 1;
|
||||
if (numsSize == 0)
|
||||
return 0;
|
||||
for (i = 1; i < numsSize; i++) {
|
||||
if(nums[i] > nums[i -1]) {
|
||||
count++;
|
||||
if (count >= maxval)
|
||||
maxval = count;
|
||||
} else {
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
return maxval;
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
struct ListNode* deleteDuplicates(struct ListNode* head) {
|
||||
if(head == NULL)
|
||||
return NULL;
|
||||
if(head->next && head->val == head->next->val) {
|
||||
/* Remove all duplicate numbers */
|
||||
while(head->next && head->val == head->next->val)
|
||||
head = head -> next;
|
||||
return deleteDuplicates(head->next);
|
||||
} else {
|
||||
head->next = deleteDuplicates(head->next);
|
||||
}
|
||||
return head;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
struct ListNode* deleteDuplicates(struct ListNode* head) {
|
||||
struct ListNode* cur = head;
|
||||
while (cur && cur->next) {
|
||||
if(cur->val == cur->next->val)
|
||||
cur->next = cur->next->next;
|
||||
else
|
||||
cur = cur->next;
|
||||
}
|
||||
return head;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
int peakIndexInMountainArray(int* A, int ASize) {
|
||||
int low = 1, high = ASize;
|
||||
while (low <= high) {
|
||||
int mid = low + (high - low) / 2;
|
||||
if (A[mid - 1] < A[mid] && A[mid] > A[mid + 1])
|
||||
return mid;
|
||||
else if(A[mid - 1] < A[mid] && A[mid] < A[mid + 1])
|
||||
low = mid + 1;
|
||||
else
|
||||
high = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
void processTraversal(struct TreeNode *root, int *res, int *size) {
|
||||
if(!root)
|
||||
return;
|
||||
processTraversal(root->left, res, size);
|
||||
res[*size] = root->val;
|
||||
*size = *size + 1;
|
||||
processTraversal(root->right, res, size);
|
||||
}
|
||||
|
||||
int* inorderTraversal(struct TreeNode* root, int* returnSize){
|
||||
int *res = malloc(256*sizeof(int));
|
||||
*returnSize = 0;
|
||||
processTraversal(root, res, returnSize);
|
||||
return res;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/* 1st way: Using 2 pointer */
|
||||
int* sortedSquares(int* A, int ASize, int* returnSize){
|
||||
int i, start = 0, end = ASize - 1;
|
||||
int *res = malloc(ASize * sizeof(int));
|
||||
*returnSize = ASize;
|
||||
for (i = ASize - 1; i >= 0; i--) {
|
||||
if(abs(A[start]) > A[end]) {
|
||||
res[i] = A[start] * A[start];
|
||||
start++;
|
||||
} else {
|
||||
res[i] = A[end] * A[end];
|
||||
end--;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 2nd way: Using qsort */
|
||||
int cmpval(const void *a, const void *b) {
|
||||
return *(int *)a - *(int *)b;
|
||||
}
|
||||
int* sortedSquares(int* A, int ASize, int* returnSize){
|
||||
int *res = malloc(ASize * sizeof(int));
|
||||
for (int i = 0; i < ASize; i++)
|
||||
res[i] = A[i] * A[i];
|
||||
*returnSize = ASize;
|
||||
qsort(res, ASize, sizeof(int), cmpval);
|
||||
return res;
|
||||
}
|
Loading…
Reference in New Issue