mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
parent
eb5bdc5449
commit
83d3234fe2
@ -49,6 +49,7 @@ LeetCode
|
||||
|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|
|
||||
|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C](./src/461.c) |Easy|
|
||||
|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|
|
||||
|
@ -1,10 +1,12 @@
|
||||
void moveZeroes(int* nums, int numsSize) {
|
||||
int i, start = 0;
|
||||
int i = 0, start = 0;
|
||||
|
||||
for (i = 0; i < numsSize; i++) {
|
||||
if (nums[i])
|
||||
nums[start++] = nums[i];
|
||||
}
|
||||
for(;start < numsSize; start++) {
|
||||
|
||||
for (start; start < numsSize; start++) {
|
||||
nums[start] = 0;
|
||||
}
|
||||
}
|
||||
|
12
leetcode/src/461.c
Normal file
12
leetcode/src/461.c
Normal file
@ -0,0 +1,12 @@
|
||||
int hammingDistance(int x, int y){
|
||||
int difference = x ^ y; //The XOR operator generates the bitwise difference in the binary representation of two numbers
|
||||
//If bit in ith position of both numbers is same, bit in difference is 0, otherwise 1
|
||||
int TotalBits = sizeof(difference)*8; //total number of bits
|
||||
int i, distance = 0;
|
||||
for(i = 0; i < TotalBits; i++) {
|
||||
if(difference & (UINT32_C(1) << i)) //if the bit on the ith position of 32 bit input is 1, then proceed
|
||||
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, as just 1 is treated as int which cannot be shifted left more than 30 times
|
||||
distance += 1;
|
||||
}
|
||||
return distance;
|
||||
}
|
@ -11,10 +11,12 @@
|
||||
struct TreeNode* searchBST(struct TreeNode* root, int val){
|
||||
if(!root)
|
||||
return NULL;
|
||||
if(root->val == val)
|
||||
|
||||
if (root->val == val) {
|
||||
return root;
|
||||
else if (root->val > val)
|
||||
} else if (root->val > val) {
|
||||
return searchBST(root->left, val);
|
||||
else
|
||||
} else {
|
||||
return searchBST(root->right, val);
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,14 @@ 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])
|
||||
if (target > nums[mid]) {
|
||||
low = mid + 1;
|
||||
else if (target < nums[mid])
|
||||
} else if (target < nums[mid]) {
|
||||
high = mid - 1;
|
||||
else
|
||||
} else {
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,12 @@
|
||||
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)
|
||||
while (head->next && head->val == head->next->val) {
|
||||
head = head -> next;
|
||||
}
|
||||
return deleteDuplicates(head->next);
|
||||
} else {
|
||||
head->next = deleteDuplicates(head->next);
|
||||
|
@ -1,9 +1,9 @@
|
||||
int rangeSumBST(struct TreeNode* root, int L, int R){
|
||||
if (root == NULL)
|
||||
if (root == NULL) {
|
||||
return 0;
|
||||
else if (root->val >= L && root->val <= R)
|
||||
} else if (root->val >= L && root->val <= R) {
|
||||
return root->val + rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);
|
||||
else
|
||||
} else {
|
||||
return rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* 1st way: Using 2 pointer */
|
||||
/* 1st way: Using 2 pointers */
|
||||
int* sortedSquares(int* A, int ASize, int* returnSize){
|
||||
int i, start = 0, end = ASize - 1;
|
||||
int *res = malloc(ASize * sizeof(int));
|
||||
@ -19,6 +19,7 @@ int* sortedSquares(int* A, int ASize, int* returnSize){
|
||||
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++)
|
||||
|
Loading…
Reference in New Issue
Block a user