Merge pull request #337 from CertifiedBlyndGuy/master

leetcode: Address readability of a few cases, and fix 283
This commit is contained in:
Hai Hoang Dang 2019-10-04 19:03:12 -07:00 committed by GitHub
commit c6f12642a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 48 additions and 40 deletions

View File

@ -10,15 +10,15 @@ int maxNumberOfBalloons(char * text){
int i, min_counter_ballons; int i, min_counter_ballons;
for (char *ptr = text; *ptr; ptr++) { for (char *ptr = text; *ptr; ptr++) {
if(*ptr == 'b') { if (*ptr == 'b') {
count_letters[0]++; count_letters[0]++;
}else if(*ptr == 'a') { } else if(*ptr == 'a') {
count_letters[1]++; count_letters[1]++;
}else if(*ptr == 'l') { } else if (*ptr == 'l') {
count_letters[2]++; count_letters[2]++;
}else if(*ptr == 'o') { } else if(*ptr == 'o') {
count_letters[3]++; count_letters[3]++;
}else if(*ptr == 'n') { } else if(*ptr == 'n') {
count_letters[4]++; 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 */ /* 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]; min_counter_ballons = count_letters[0];
for(i = 1; i < 5; i++){ for (i = 1; i < 5; i++) {
if(count_letters[i] < min_counter_ballons) if (count_letters[i] < min_counter_ballons)
min_counter_ballons = count_letters[i]; min_counter_ballons = count_letters[i];
} }
return min_counter_ballons; return min_counter_ballons;
} }

View File

@ -1,10 +1,12 @@
void moveZeroes(int* nums, int numsSize){ void moveZeroes(int* nums, int numsSize) {
int i, start = 0; int i = 0, start = 0;
for (i = 0; i < numsSize; i++) { for (i = 0; i < numsSize; i++) {
if(nums[i]) if (nums[i])
nums[start++] = nums[i]; nums[start++] = nums[i];
} }
for(;start < numsSize; start++) {
for (start; start < numsSize; start++) {
nums[start] = 0; nums[start] = 0;
} }
} }

View File

@ -6,7 +6,7 @@ struct TreeNode * newNode (int item) {
} }
struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2){ struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2){
if(t1 == NULL && t2 == NULL) if (t1 == NULL && t2 == NULL)
return NULL; return NULL;
int item = (t1 == NULL ? 0 : t1->val) + (t2 == NULL ? 0 : t2->val); int item = (t1 == NULL ? 0 : t1->val) + (t2 == NULL ? 0 : t2->val);
struct TreeNode *node = newNode(item); struct TreeNode *node = newNode(item);

View File

@ -11,10 +11,12 @@
struct TreeNode* searchBST(struct TreeNode* root, int val){ struct TreeNode* searchBST(struct TreeNode* root, int val){
if(!root) if(!root)
return NULL; return NULL;
if(root->val == val)
if (root->val == val) {
return root; return root;
else if (root->val > val) } else if (root->val > val) {
return searchBST(root->left, val); return searchBST(root->left, val);
else } else {
return searchBST(root->right, val); return searchBST(root->right, val);
}
} }

View File

@ -2,12 +2,13 @@ int search(int* nums, int numsSize, int target){
int low = 0, high = numsSize - 1; int low = 0, high = numsSize - 1;
while (low <= high) { while (low <= high) {
int mid = low + (high - low) / 2; int mid = low + (high - low) / 2;
if (target > nums[mid]) if (target > nums[mid]) {
low = mid + 1; low = mid + 1;
else if (target < nums[mid]) } else if (target < nums[mid]) {
high = mid - 1; high = mid - 1;
else } else {
return mid; return mid;
}
} }
return -1; return -1;
} }

View File

@ -1,18 +1,18 @@
// for strlen( ) // for strlen()
#include<string.h> #include <string.h>
int numJewelsInStones(char * j, char * s){ int numJewelsInStones(char * j, char * s) {
// as strlen is O(n), store it once rather than using it in for loop // 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; int cnt[500], lens = strlen(s), lenj = strlen(j), sol = 0;
memset(cnt,0,sizeof(cnt)); memset(cnt, 0, sizeof(cnt));
// lookup to know which character occurs in j // lookup to know which character occurs in j
for(int i=0;i<lenj;i++) for (int i = 0; i < lenj; i++)
cnt[j[i]]++; cnt[j[i]]++;
// count the characters in s // count the characters in s
for(int i=0;i<lens;i++) for (int i = 0; i < lens; i++)
sol+=cnt[s[i]]; sol += cnt[s[i]];
return sol; return sol;
} }

View File

@ -1,10 +1,12 @@
struct ListNode* deleteDuplicates(struct ListNode* head) { struct ListNode* deleteDuplicates(struct ListNode* head) {
if(head == NULL) if (head == NULL)
return NULL; return NULL;
if(head->next && head->val == head->next->val) {
if (head->next && head->val == head->next->val) {
/* Remove all duplicate numbers */ /* Remove all duplicate numbers */
while(head->next && head->val == head->next->val) while (head->next && head->val == head->next->val) {
head = head -> next; head = head -> next;
}
return deleteDuplicates(head->next); return deleteDuplicates(head->next);
} else { } else {
head->next = deleteDuplicates(head->next); head->next = deleteDuplicates(head->next);

View File

@ -10,13 +10,13 @@
* *
* Note: The returned array must be malloced, assume caller calls free(). * 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 *retArr = malloc(ASize * sizeof(int));
int oddIndex = ASize - 1; int oddIndex = ASize - 1;
int evenIndex = 0; int evenIndex = 0;
*returnSize = ASize; *returnSize = ASize;
for (int i = 0; i < ASize; i++) { for (int i = 0; i < ASize; i++) {
if(A[i] % 2 == 0) { if (A[i] % 2 == 0) {
retArr[evenIndex] = A[i]; retArr[evenIndex] = A[i];
evenIndex++; evenIndex++;
} else { } else {

View File

@ -1,9 +1,9 @@
int rangeSumBST(struct TreeNode* root, int L, int R){ int rangeSumBST(struct TreeNode* root, int L, int R){
if (root == NULL) if (root == NULL) {
return 0; 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); 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); return rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);
}
} }

View File

@ -1,10 +1,10 @@
/* 1st way: Using 2 pointer */ /* 1st way: Using 2 pointers */
int* sortedSquares(int* A, int ASize, int* returnSize){ int* sortedSquares(int* A, int ASize, int* returnSize){
int i, start = 0, end = ASize - 1; int i, start = 0, end = ASize - 1;
int *res = malloc(ASize * sizeof(int)); int *res = malloc(ASize * sizeof(int));
*returnSize = ASize; *returnSize = ASize;
for (i = ASize - 1; i >= 0; i--) { 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]; res[i] = A[start] * A[start];
start++; start++;
} else { } else {
@ -19,7 +19,8 @@ int* sortedSquares(int* A, int ASize, int* returnSize){
int cmpval(const void *a, const void *b) { int cmpval(const void *a, const void *b) {
return *(int *)a - *(int *)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)); int *res = malloc(ASize * sizeof(int));
for (int i = 0; i < ASize; i++) for (int i = 0; i < ASize; i++)
res[i] = A[i] * A[i]; res[i] = A[i] * A[i];