From 83d3234fe29b635d76d44a14b3db3849c6a8e10b Mon Sep 17 00:00:00 2001 From: leoperd <56191198+leoperd@users.noreply.github.com> Date: Sat, 12 Oct 2019 15:40:42 +0530 Subject: [PATCH] Update BubbleSort.c (#343) Update BubbleSort.c --- leetcode/README.md | 1 + leetcode/src/1189.c | 16 ++++++++-------- leetcode/src/283.c | 10 ++++++---- leetcode/src/461.c | 12 ++++++++++++ leetcode/src/617.c | 2 +- leetcode/src/700.c | 8 +++++--- leetcode/src/704.c | 7 ++++--- leetcode/src/771.c | 18 +++++++++--------- leetcode/src/82.c | 8 +++++--- leetcode/src/905.c | 4 ++-- leetcode/src/938.c | 8 ++++---- leetcode/src/977.c | 7 ++++--- 12 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 leetcode/src/461.c diff --git a/leetcode/README.md b/leetcode/README.md index 7f8d31f3..b0a8e0cc 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -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| diff --git a/leetcode/src/1189.c b/leetcode/src/1189.c index 486b09ad..030ba243 100644 --- a/leetcode/src/1189.c +++ b/leetcode/src/1189.c @@ -10,15 +10,15 @@ int maxNumberOfBalloons(char * text){ int i, min_counter_ballons; for (char *ptr = text; *ptr; ptr++) { - if(*ptr == 'b') { + if (*ptr == 'b') { count_letters[0]++; - }else if(*ptr == 'a') { + } else if(*ptr == 'a') { count_letters[1]++; - }else if(*ptr == 'l') { + } else if (*ptr == 'l') { count_letters[2]++; - }else if(*ptr == 'o') { + } else if(*ptr == 'o') { count_letters[3]++; - }else if(*ptr == 'n') { + } else if(*ptr == 'n') { count_letters[4]++; } } @@ -29,10 +29,10 @@ int maxNumberOfBalloons(char * text){ /* Max number of times which we can write ballon is equal to min value of letters on count_letter */ min_counter_ballons = count_letters[0]; - for(i = 1; i < 5; i++){ - if(count_letters[i] < min_counter_ballons) + for (i = 1; i < 5; i++) { + if (count_letters[i] < min_counter_ballons) min_counter_ballons = count_letters[i]; } return min_counter_ballons; -} \ No newline at end of file +} diff --git a/leetcode/src/283.c b/leetcode/src/283.c index 9eebe6a9..67cfc7d9 100644 --- a/leetcode/src/283.c +++ b/leetcode/src/283.c @@ -1,10 +1,12 @@ -void moveZeroes(int* nums, int numsSize){ - int i, start = 0; +void moveZeroes(int* nums, int numsSize) { + int i = 0, start = 0; + for (i = 0; i < numsSize; i++) { - if(nums[i]) + if (nums[i]) nums[start++] = nums[i]; } - for(;start < numsSize; start++) { + + for (start; start < numsSize; start++) { nums[start] = 0; } } diff --git a/leetcode/src/461.c b/leetcode/src/461.c new file mode 100644 index 00000000..a3ac8f99 --- /dev/null +++ b/leetcode/src/461.c @@ -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; +} \ No newline at end of file diff --git a/leetcode/src/617.c b/leetcode/src/617.c index 64417309..1a6da56d 100644 --- a/leetcode/src/617.c +++ b/leetcode/src/617.c @@ -6,7 +6,7 @@ struct TreeNode * newNode (int item) { } struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2){ - if(t1 == NULL && t2 == NULL) + if (t1 == NULL && t2 == NULL) return NULL; int item = (t1 == NULL ? 0 : t1->val) + (t2 == NULL ? 0 : t2->val); struct TreeNode *node = newNode(item); diff --git a/leetcode/src/700.c b/leetcode/src/700.c index 7217117f..c8ce6335 100644 --- a/leetcode/src/700.c +++ b/leetcode/src/700.c @@ -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); + } } diff --git a/leetcode/src/704.c b/leetcode/src/704.c index fde40fc5..28921745 100644 --- a/leetcode/src/704.c +++ b/leetcode/src/704.c @@ -2,12 +2,13 @@ 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; } diff --git a/leetcode/src/771.c b/leetcode/src/771.c index 76a49d53..6fca4d27 100644 --- a/leetcode/src/771.c +++ b/leetcode/src/771.c @@ -1,18 +1,18 @@ -// for strlen( ) -#include +// for strlen() +#include -int numJewelsInStones(char * j, char * s){ - // as strlen is O(n), store it once rather than using it in for loop - int cnt[500],lens=strlen(s),lenj=strlen(j),sol=0; - memset(cnt,0,sizeof(cnt)); +int numJewelsInStones(char * j, char * s) { + // as strlen is O(n), store it once rather than using it in for loop + int cnt[500], lens = strlen(s), lenj = strlen(j), sol = 0; + memset(cnt, 0, sizeof(cnt)); // lookup to know which character occurs in j - for(int i=0;inext && head->val == head->next->val) { + + 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); diff --git a/leetcode/src/905.c b/leetcode/src/905.c index bae5aec6..f6cb7015 100644 --- a/leetcode/src/905.c +++ b/leetcode/src/905.c @@ -10,13 +10,13 @@ * * Note: The returned array must be malloced, assume caller calls free(). */ -int* sortArrayByParity(int* A, int ASize, int* returnSize){ +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) { + if (A[i] % 2 == 0) { retArr[evenIndex] = A[i]; evenIndex++; } else { diff --git a/leetcode/src/938.c b/leetcode/src/938.c index 87e3d998..e1f84763 100644 --- a/leetcode/src/938.c +++ b/leetcode/src/938.c @@ -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); - + } } diff --git a/leetcode/src/977.c b/leetcode/src/977.c index c0663504..93fb8100 100644 --- a/leetcode/src/977.c +++ b/leetcode/src/977.c @@ -1,10 +1,10 @@ -/* 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)); *returnSize = ASize; for (i = ASize - 1; i >= 0; i--) { - if(abs(A[start]) > A[end]) { + if (abs(A[start]) > A[end]) { res[i] = A[start] * A[start]; start++; } else { @@ -19,7 +19,8 @@ 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* 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];