├── 1.c ├── 101.c ├── 104.c ├── 108.c ├── 1089.c ├── 109.c ├── 11.c ├── 110.c ├── 112.c ├── 1184.c ├── 1189.c ├── 12.c ├── 1207.c ├── 121.c ├── 125.c ├── 13.c ├── 136.c ├── 141.c ├── 142.c ├── 153.c ├── 160.c ├── 169.c ├── 173.c ├── 189.c ├── 190.c ├── 191.c ├── 2.c ├── 20.c ├── 201.c ├── 203.c ├── 206.c ├── 21.c ├── 215.c ├── 217.c ├── 226.c ├── 231.c ├── 234.c ├── 24.c ├── 242.c ├── 26.c ├── 268.c ├── 27.c ├── 278.c ├── 28.c ├── 283.c ├── 287.c ├── 29.c ├── 3.c ├── 344.c ├── 35.c ├── 367.c ├── 38.c ├── 387.c ├── 389.c ├── 4.c ├── 404.c ├── 442.c ├── 461.c ├── 476.c ├── 509.c ├── 520.c ├── 53.c ├── 561.c ├── 617.c ├── 647.c ├── 66.c ├── 674.c ├── 7.c ├── 700.c ├── 701.c ├── 704.c ├── 709.c ├── 771.c ├── 8.c ├── 82.c ├── 83.c ├── 852.c ├── 876.c ├── 9.c ├── 905.c ├── 917.c ├── 938.c ├── 94.c ├── 965.c ├── 977.c └── README.md /1.c: -------------------------------------------------------------------------------- 1 | int *twoSum(int *nums, int numsSize, int target, int *returnSize) 2 | { 3 | int i, j; 4 | int *ret = calloc(2, sizeof(int)); 5 | for (i = 0; i < numsSize; i++) 6 | { 7 | int key = target - nums[i]; 8 | for (j = i + 1; j < numsSize; j++) 9 | if (nums[j] == key) 10 | { 11 | ret[0] = i; 12 | ret[1] = j; 13 | } 14 | } 15 | *returnSize = 2; 16 | return ret; 17 | } 18 | -------------------------------------------------------------------------------- /101.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * struct TreeNode *left; 6 | * struct TreeNode *right; 7 | * }; 8 | */ 9 | 10 | bool checkSymmetric(struct TreeNode *left, struct TreeNode *right) 11 | { 12 | if (!left || !right) 13 | return left == right; 14 | if (left->val != right->val) 15 | return 0; 16 | return checkSymmetric(left->left, right->right) && 17 | checkSymmetric(left->right, right->left); 18 | } 19 | 20 | bool isSymmetric(struct TreeNode *root) 21 | { 22 | return root == NULL || checkSymmetric(root->left, root->right); 23 | } 24 | -------------------------------------------------------------------------------- /104.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * struct TreeNode *left; 6 | * struct TreeNode *right; 7 | * }; 8 | */ 9 | 10 | int maxval(int a, int b) 11 | { 12 | if (a > b) 13 | return a; 14 | else 15 | return b; 16 | } 17 | int maxDepth(struct TreeNode *root) 18 | { 19 | if (root == NULL) 20 | return 0; 21 | else 22 | return 1 + maxval(maxDepth(root->left), maxDepth(root->right)); 23 | } 24 | -------------------------------------------------------------------------------- /108.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * struct TreeNode *left; 6 | * struct TreeNode *right; 7 | * }; 8 | */ 9 | 10 | struct TreeNode *convertBST(int *nums, int left, int right) 11 | { 12 | if (left > right) 13 | return NULL; 14 | else 15 | { 16 | int mid = (right + left) / 2; 17 | struct TreeNode *new_val = malloc(sizeof(struct TreeNode)); 18 | new_val->val = nums[mid]; 19 | new_val->left = convertBST(nums, left, mid - 1); 20 | new_val->right = convertBST(nums, mid + 1, right); 21 | return new_val; 22 | } 23 | } 24 | 25 | struct TreeNode *sortedArrayToBST(int *nums, int numsSize) 26 | { 27 | if (numsSize == 0) 28 | return NULL; 29 | else 30 | return convertBST(nums, 0, numsSize - 1); 31 | } 32 | -------------------------------------------------------------------------------- /1089.c: -------------------------------------------------------------------------------- 1 | void duplicateZeros(int *arr, int arrSize) 2 | { 3 | int i, start = 0; 4 | int *tmp = malloc(arrSize * sizeof(int)); 5 | /* Copy arr into tmp arr */ 6 | for (i = 0; i < arrSize; i++) 7 | { 8 | tmp[i] = arr[i]; 9 | } 10 | i = 0; 11 | for (start = 0; start < arrSize; start++) 12 | { 13 | arr[start] = tmp[i]; 14 | if (tmp[i] == 0) 15 | { 16 | start++; 17 | if (start < arrSize) 18 | arr[start] = 0; 19 | } 20 | i++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /109.c: -------------------------------------------------------------------------------- 1 | struct TreeNode *buildBST(struct ListNode *head, struct ListNode *tail) 2 | { 3 | if (head == tail) 4 | return NULL; 5 | struct ListNode *slow = head, *fast = head; 6 | while (fast != tail && fast->next != tail) 7 | { 8 | fast = fast->next->next; 9 | slow = slow->next; 10 | } 11 | struct TreeNode *node = malloc(sizeof(struct TreeNode)); 12 | node->val = slow->val; 13 | node->left = buildBST(head, slow); 14 | node->right = buildBST(slow->next, tail); 15 | return node; 16 | } 17 | struct TreeNode *sortedListToBST(struct ListNode *head) 18 | { 19 | if (!head) 20 | return NULL; 21 | else 22 | return buildBST(head, NULL); 23 | } 24 | -------------------------------------------------------------------------------- /11.c: -------------------------------------------------------------------------------- 1 | // Fucntion to calculate min of values a and b 2 | int min(int a, int b) { return ((a < b) ? a : b); } 3 | 4 | // Two pointer approach to find maximum container area 5 | int maxArea(int *height, int heightSize) 6 | { 7 | // Start with maximum container width 8 | int start = 0; 9 | int end = heightSize - 1; 10 | int res = 0; 11 | 12 | while (start < end) 13 | { 14 | // Calculate current area by taking minimum of two heights 15 | int currArea = (end - start) * min(height[start], height[end]); 16 | 17 | if (currArea > res) 18 | res = currArea; 19 | 20 | if (height[start] < height[end]) 21 | start = start + 1; 22 | else 23 | end = end - 1; 24 | } 25 | 26 | return res; 27 | } 28 | -------------------------------------------------------------------------------- /110.c: -------------------------------------------------------------------------------- 1 | int max(int a, int b) { return a >= b ? a : b; } 2 | 3 | int height(struct TreeNode *root) 4 | { 5 | if (root == NULL) 6 | return 0; 7 | else 8 | return 1 + max(height(root->left), height(root->right)); 9 | } 10 | 11 | bool isBalanced(struct TreeNode *root) 12 | { 13 | if (root == NULL) 14 | return 1; 15 | int left = height(root->left); 16 | int right = height(root->right); 17 | return abs(left - right) <= 1 && isBalanced(root->left) && 18 | isBalanced(root->right); 19 | } 20 | -------------------------------------------------------------------------------- /112.c: -------------------------------------------------------------------------------- 1 | bool hasPathSum(struct TreeNode *root, int sum) 2 | { 3 | if (root == NULL) 4 | return 0; 5 | if (!root->left && !root->right && sum - root->val == 0) 6 | return 1; 7 | return hasPathSum(root->left, sum - root->val) || 8 | hasPathSum(root->right, sum - root->val); 9 | } 10 | -------------------------------------------------------------------------------- /1184.c: -------------------------------------------------------------------------------- 1 | int distanceBetweenBusStops(int *distance, int distanceSize, int start, 2 | int destination) 3 | { 4 | int sum1 = 0, sum2 = 0; 5 | if (start > destination) 6 | { 7 | int tmp = start; 8 | start = destination; 9 | destination = tmp; 10 | } 11 | for (auto i = 0; i < distanceSize; ++i) 12 | { 13 | if (i >= start && i < destination) 14 | sum1 += distance[i]; 15 | else 16 | sum2 += distance[i]; 17 | } 18 | return sum1 < sum2 ? sum1 : sum2; 19 | } 20 | -------------------------------------------------------------------------------- /1189.c: -------------------------------------------------------------------------------- 1 | int maxNumberOfBalloons(char *text) 2 | { 3 | /* 4 | 0 -> b, 5 | 1 -> a, 6 | 2 -> l, 7 | 3 -> o, 8 | 4 -> n 9 | */ 10 | int count_letters[5] = {0}; 11 | int i, min_counter_ballons; 12 | 13 | for (char *ptr = text; *ptr; ptr++) 14 | { 15 | if (*ptr == 'b') 16 | { 17 | count_letters[0]++; 18 | } 19 | else if (*ptr == 'a') 20 | { 21 | count_letters[1]++; 22 | } 23 | else if (*ptr == 'l') 24 | { 25 | count_letters[2]++; 26 | } 27 | else if (*ptr == 'o') 28 | { 29 | count_letters[3]++; 30 | } 31 | else if (*ptr == 'n') 32 | { 33 | count_letters[4]++; 34 | } 35 | } 36 | 37 | /* Divide by 2 the repeted letters */ 38 | count_letters[2] /= 2; 39 | count_letters[3] /= 2; 40 | 41 | /* Max number of times which we can write ballon is equal to min value of 42 | * letters on count_letter */ 43 | min_counter_ballons = count_letters[0]; 44 | for (i = 1; i < 5; i++) 45 | { 46 | if (count_letters[i] < min_counter_ballons) 47 | min_counter_ballons = count_letters[i]; 48 | } 49 | 50 | return min_counter_ballons; 51 | } 52 | -------------------------------------------------------------------------------- /12.c: -------------------------------------------------------------------------------- 1 | char *getOne(char c) 2 | { 3 | switch (c) 4 | { 5 | case '9': 6 | return "IX"; 7 | 8 | case '8': 9 | return "VIII"; 10 | 11 | case '7': 12 | return "VII"; 13 | 14 | case '6': 15 | return "VI"; 16 | 17 | case '5': 18 | return "V"; 19 | 20 | case '4': 21 | return "IV"; 22 | 23 | case '3': 24 | return "III"; 25 | 26 | case '2': 27 | return "II"; 28 | 29 | case '1': 30 | return "I"; 31 | 32 | case '0': 33 | return ""; 34 | 35 | default: 36 | return NULL; 37 | } 38 | } 39 | 40 | char *getTen(char c) 41 | { 42 | switch (c) 43 | { 44 | case '9': 45 | return "XC"; 46 | 47 | case '8': 48 | return "LXXX"; 49 | 50 | case '7': 51 | return "LXX"; 52 | 53 | case '6': 54 | return "LX"; 55 | 56 | case '5': 57 | return "L"; 58 | 59 | case '4': 60 | return "XL"; 61 | 62 | case '3': 63 | return "XXX"; 64 | 65 | case '2': 66 | return "XX"; 67 | 68 | case '1': 69 | return "X"; 70 | 71 | case '0': 72 | return ""; 73 | 74 | default: 75 | return NULL; 76 | } 77 | } 78 | 79 | char *getHundred(char c) 80 | { 81 | switch (c) 82 | { 83 | case '9': 84 | return "CM"; 85 | 86 | case '8': 87 | return "DCCC"; 88 | 89 | case '7': 90 | return "DCC"; 91 | 92 | case '6': 93 | return "DC"; 94 | 95 | case '5': 96 | return "D"; 97 | 98 | case '4': 99 | return "CD"; 100 | 101 | case '3': 102 | return "CCC"; 103 | 104 | case '2': 105 | return "CC"; 106 | 107 | case '1': 108 | return "C"; 109 | 110 | case '0': 111 | return ""; 112 | 113 | default: 114 | return NULL; 115 | } 116 | } 117 | 118 | char *getThousand(char c) 119 | { 120 | switch (c) 121 | { 122 | case '3': 123 | return "MMM"; 124 | 125 | case '2': 126 | return "MM"; 127 | 128 | case '1': 129 | return "M"; 130 | 131 | default: 132 | return NULL; 133 | } 134 | } 135 | 136 | char *intToRoman(int num) 137 | { 138 | int length; 139 | char number[5]; 140 | char *s = malloc(16 * sizeof(char)); 141 | 142 | sprintf(number, "%i", num); 143 | 144 | length = strlen(number); 145 | 146 | switch (length) 147 | { 148 | case 4: 149 | sprintf(s, "%s%s%s%s", getThousand(number[0]), getHundred(number[1]), 150 | getTen(number[2]), getOne(number[3])); 151 | break; 152 | 153 | case 3: 154 | sprintf(s, "%s%s%s", getHundred(number[0]), getTen(number[1]), 155 | getOne(number[2])); 156 | 157 | break; 158 | 159 | case 2: 160 | sprintf(s, "%s%s", getTen(number[0]), getOne(number[1])); 161 | 162 | break; 163 | 164 | case 1: 165 | s = getOne(number[0]); 166 | break; 167 | 168 | default: 169 | break; 170 | } 171 | return s; 172 | } 173 | -------------------------------------------------------------------------------- /1207.c: -------------------------------------------------------------------------------- 1 | #define MAP_SIZE 2048 2 | 3 | int cmpvalue(const void *a, const void *b) { return *(int *)b - *(int *)a; } 4 | bool uniqueOccurrences(int *arr, int arrSize) 5 | { 6 | int *map = calloc(MAP_SIZE, sizeof(int)); 7 | int i; 8 | for (i = 0; i < arrSize; i++) 9 | { 10 | if (arr[i] < 0) 11 | map[arr[i] + MAP_SIZE / 2] += 1; 12 | else 13 | map[arr[i]] += 1; 14 | } 15 | /* number of occurrences is sorted by decreasing order 16 | Ex: 3 2 1 0 0 0 0 */ 17 | qsort(map, MAP_SIZE, sizeof(int), cmpvalue); 18 | i = 0; 19 | while (map[i]) 20 | { 21 | if (map[i] == map[i + 1]) 22 | return 0; 23 | i++; 24 | } 25 | return 1; 26 | } 27 | -------------------------------------------------------------------------------- /121.c: -------------------------------------------------------------------------------- 1 | int maxcmp(int a, int b) { return (a >= b) ? a : b; } 2 | 3 | /* max subarray problem by using Kadane's Algorithm 4 | */ 5 | int maxProfit(int *prices, int pricesSize) 6 | { 7 | /* maxCur: current maximum 8 | * maxSoFar: found maximum for subarray so far 9 | */ 10 | int maxCur = 0, maxSoFar = 0; 11 | for (int i = 1; i < pricesSize; i++) 12 | { 13 | maxCur = maxcmp(0, maxCur + prices[i] - prices[i - 1]); 14 | maxSoFar = maxcmp(maxSoFar, maxCur); 15 | } 16 | return maxSoFar; 17 | } 18 | -------------------------------------------------------------------------------- /125.c: -------------------------------------------------------------------------------- 1 | bool isPalindrome(char *s) 2 | { 3 | int start = 0, end = strlen(s) - 1; 4 | while (start < end) 5 | { 6 | if (!isalpha(s[start]) && !isalnum(s[start])) 7 | { 8 | start++; 9 | } 10 | else if (!isalpha(s[end]) && !isalnum(s[end])) 11 | { 12 | end--; 13 | } 14 | else 15 | { 16 | char c1 = tolower(s[start]); 17 | char c2 = tolower(s[end]); 18 | if (c1 != c2) 19 | return 0; 20 | start++; 21 | end--; 22 | } 23 | } 24 | return 1; 25 | } 26 | -------------------------------------------------------------------------------- /13.c: -------------------------------------------------------------------------------- 1 | int romanToInt(char *s) 2 | { 3 | int romanToInt = 0; 4 | for (int i = 0; i < strlen(s); i++) 5 | { 6 | switch (s[i]) 7 | { 8 | case 'I': 9 | if (i + 1 < strlen(s)) 10 | { 11 | if (s[i + 1] == 'V' || s[i + 1] == 'X') 12 | { 13 | romanToInt -= 1; 14 | break; 15 | } 16 | } 17 | romanToInt += 1; 18 | break; 19 | case 'V': 20 | romanToInt += 5; 21 | break; 22 | case 'X': 23 | if (i + 1 < strlen(s)) 24 | { 25 | if (s[i + 1] == 'L' || s[i + 1] == 'C') 26 | { 27 | romanToInt -= 10; 28 | break; 29 | } 30 | } 31 | romanToInt += 10; 32 | break; 33 | case 'L': 34 | romanToInt += 50; 35 | break; 36 | case 'C': 37 | if (i + 1 < strlen(s)) 38 | { 39 | if (s[i + 1] == 'D' || s[i + 1] == 'M') 40 | { 41 | romanToInt -= 100; 42 | break; 43 | } 44 | } 45 | romanToInt += 100; 46 | break; 47 | case 'D': 48 | romanToInt += 500; 49 | break; 50 | case 'M': 51 | romanToInt += 1000; 52 | break; 53 | default: 54 | break; 55 | } 56 | } 57 | return romanToInt; 58 | } -------------------------------------------------------------------------------- /136.c: -------------------------------------------------------------------------------- 1 | int singleNumber(int *nums, int numsSize) 2 | { 3 | int i, result = 0; 4 | for (i = 0; i < numsSize; i++) result = result ^ nums[i]; 5 | return result; 6 | } 7 | -------------------------------------------------------------------------------- /141.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * struct ListNode *next; 6 | * }; 7 | */ 8 | bool hasCycle(struct ListNode *head) 9 | { 10 | struct ListNode *fast = head, *slow = head; 11 | while (slow && fast && fast->next) 12 | { 13 | fast = fast->next->next; 14 | slow = slow->next; 15 | if (fast == slow) 16 | return true; 17 | } 18 | return false; 19 | } 20 | -------------------------------------------------------------------------------- /142.c: -------------------------------------------------------------------------------- 1 | struct ListNode *detectCycle(struct ListNode *head) 2 | { 3 | if (head == NULL || head->next == NULL) 4 | return NULL; 5 | struct ListNode *slow, *fast; 6 | slow = fast = head; 7 | while (fast && fast->next) 8 | { 9 | slow = slow->next; 10 | fast = fast->next->next; 11 | if (slow == fast) 12 | { 13 | struct ListNode *entry = head; 14 | while (slow != entry) 15 | { 16 | slow = slow->next; 17 | entry = entry->next; 18 | } 19 | return entry; 20 | } 21 | } 22 | return NULL; 23 | } 24 | -------------------------------------------------------------------------------- /153.c: -------------------------------------------------------------------------------- 1 | int findMin(int *nums, int numsSize) 2 | { 3 | int low = 0, high = numsSize - 1; 4 | while (low < high) 5 | { 6 | int mid = low + (high - low) / 2; 7 | /* minimum is on left side */ 8 | if (nums[mid] < nums[high]) 9 | high = mid; 10 | /* minimum is on right side */ 11 | else 12 | low = mid + 1; 13 | } 14 | return nums[low]; 15 | } 16 | -------------------------------------------------------------------------------- /160.c: -------------------------------------------------------------------------------- 1 | struct ListNode *getIntersectionNode(struct ListNode *headA, 2 | struct ListNode *headB) 3 | { 4 | struct ListNode *cur1 = headA, *cur2 = headB; 5 | if (cur1 == NULL || cur2 == NULL) 6 | return NULL; 7 | while (cur1 && cur2 && cur1 != cur2) 8 | { 9 | cur1 = cur1->next; 10 | cur2 = cur2->next; 11 | if (cur1 == cur2) 12 | return cur1; 13 | if (!cur1) 14 | cur1 = headB; 15 | if (!cur2) 16 | cur2 = headA; 17 | } 18 | return cur1; 19 | } 20 | -------------------------------------------------------------------------------- /169.c: -------------------------------------------------------------------------------- 1 | /* Boyer-Moore Majority Vote Algorithm 2 | * http://www.cs.utexas.edu/~moore/best-ideas/mjrty/ */ 3 | int majorityElement(int *nums, int numsSize) 4 | { 5 | int count = 1; 6 | int majorNum = nums[0]; 7 | for (int i = 1; i < numsSize; i++) 8 | { 9 | if (count == 0) 10 | { 11 | majorNum = nums[i]; 12 | count++; 13 | } 14 | else if (majorNum == nums[i]) 15 | count++; 16 | else 17 | count--; 18 | } 19 | return majorNum; 20 | } 21 | -------------------------------------------------------------------------------- /173.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * struct TreeNode *left; 6 | * struct TreeNode *right; 7 | * }; 8 | */ 9 | 10 | #include 11 | 12 | typedef struct 13 | { 14 | int *values; 15 | int CurrentIndex; 16 | int NumberOfNodes; 17 | } BSTIterator; 18 | 19 | void TraverseAndAssign(struct TreeNode *root, BSTIterator *obj) 20 | { 21 | if (!root) 22 | return; 23 | if (root->left) 24 | TraverseAndAssign(root->left, obj); 25 | obj->values[obj->CurrentIndex] = root->val; 26 | obj->CurrentIndex++; 27 | if (root->right) 28 | TraverseAndAssign(root->right, obj); 29 | } 30 | 31 | int TotalNodes(struct TreeNode *root) 32 | { 33 | if (!root) 34 | return 0; 35 | int nodes_left = TotalNodes(root->left); 36 | int nodes_right = TotalNodes(root->right); 37 | return nodes_left + nodes_right + 1; 38 | } 39 | 40 | BSTIterator *bSTIteratorCreate(struct TreeNode *root) 41 | { 42 | int n = TotalNodes(root); 43 | int size = n + 1; 44 | printf("%d", size); 45 | BSTIterator *obj = (BSTIterator *)malloc(sizeof(BSTIterator)); 46 | obj->values = (int *)calloc(size, sizeof(int)); 47 | obj->CurrentIndex = 0; 48 | obj->NumberOfNodes = n; 49 | obj->values[size - 1] = INT_MAX; 50 | TraverseAndAssign(root, obj); 51 | obj->CurrentIndex = 0; 52 | return obj; 53 | } 54 | 55 | /** @return the next smallest number */ 56 | int bSTIteratorNext(BSTIterator *obj) 57 | { 58 | int NextValue = obj->values[obj->CurrentIndex]; 59 | obj->CurrentIndex++; 60 | return NextValue; 61 | } 62 | 63 | /** @return whether we have a next smallest number */ 64 | bool bSTIteratorHasNext(BSTIterator *obj) 65 | { 66 | if (!obj->NumberOfNodes) 67 | { 68 | return false; 69 | } 70 | printf(" Here "); 71 | return (obj->values[obj->CurrentIndex] == INT_MAX) ? false : true; 72 | } 73 | 74 | void bSTIteratorFree(BSTIterator *obj) 75 | { 76 | free(obj->values); 77 | free(obj); 78 | } 79 | -------------------------------------------------------------------------------- /189.c: -------------------------------------------------------------------------------- 1 | void rotate(int *nums, int numsSize, int k) 2 | { 3 | for (int i = 1; i <= k; i++) 4 | { 5 | int j; 6 | int lastElement; 7 | lastElement = nums[numsSize - 1]; 8 | for (j = numsSize - 1; j > 0; j--) 9 | { 10 | nums[j] = nums[j - 1]; 11 | } 12 | nums[0] = lastElement; 13 | } 14 | } -------------------------------------------------------------------------------- /190.c: -------------------------------------------------------------------------------- 1 | uint32_t reverseBits(uint32_t n) 2 | { 3 | uint TotalBits = 32; 4 | uint32_t reverse_int = 0; // stored in memory as 32 bits, each bit valued 0 5 | uint i; 6 | for (i = 0; i < TotalBits; i++) 7 | { 8 | if ((n & (UINT32_C(1) 9 | << i))) // if the bit on the ith position of 32 bit input is 10 | // 1, then proceed Further note the use of UINT32_C 11 | // to convert 1 to unsigned 32 bit int, since just 1 12 | // is treated as int which cannot be shifted left 13 | // more than 30 times 14 | reverse_int = 15 | reverse_int | 16 | (UINT32_C(1) 17 | << (TotalBits - 1 - 18 | i)); // Convert the ith bit from the end in reverse_int 19 | // from 0 to 1, if ith bit from beginning in n is 1 20 | // This is achieved by using bitwise OR on 21 | // reverse_int (where ith bit from end is currently 22 | // 0) and 1 shifted left 31 - i bits (to ith bit from 23 | // the end) 24 | } 25 | return reverse_int; 26 | } -------------------------------------------------------------------------------- /191.c: -------------------------------------------------------------------------------- 1 | int hammingWeight(uint32_t n) 2 | { 3 | int TotalBits = 32; 4 | int i, weight = 0; 5 | for (i = 0; i < TotalBits; i++) 6 | { 7 | if (n & 8 | (UINT32_C(1) 9 | << i)) // if the bit on the ith position of 32 bit input is 1, 10 | // then proceed Further note the use of UINT32_C to 11 | // convert 1 to unsigned 32 bit int, as just 1 is treated 12 | // as int which cannot be shifted left more than 30 times 13 | weight += 1; 14 | } 15 | return weight; 16 | } -------------------------------------------------------------------------------- /2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * struct ListNode *next; 6 | * }; 7 | */ 8 | 9 | struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) 10 | { 11 | struct ListNode *head = NULL; 12 | struct ListNode *walk = NULL; 13 | struct ListNode *tmp = NULL; 14 | 15 | int carry = 0; 16 | int val1 = 0; 17 | int val2 = 0; 18 | int val = 0; 19 | 20 | while (l1 != NULL || l2 != NULL || carry) 21 | { 22 | val1 = 0; 23 | val2 = 0; 24 | val = 0; 25 | 26 | if (l1) 27 | { 28 | val1 = l1->val; 29 | l1 = l1->next; 30 | } 31 | 32 | if (l2) 33 | { 34 | val2 = l2->val; 35 | l2 = l2->next; 36 | } 37 | 38 | val = carry + val1 + val2; 39 | carry = val / 10; 40 | 41 | tmp = malloc(sizeof(struct ListNode)); 42 | tmp->val = val % 10; 43 | tmp->next = NULL; 44 | 45 | if (!head) 46 | { 47 | head = walk = tmp; 48 | } 49 | else 50 | { 51 | walk->next = tmp; 52 | walk = walk->next; 53 | } 54 | } 55 | 56 | return head; 57 | } 58 | -------------------------------------------------------------------------------- /20.c: -------------------------------------------------------------------------------- 1 | bool isValid(char *s) 2 | { 3 | int i, k = 0, len = strlen(s); 4 | char *store = calloc(len, sizeof(char)); 5 | 6 | for (i = 0; s[i] != '\0'; i++) 7 | { 8 | switch (s[i]) 9 | { 10 | case '(': 11 | case '{': 12 | case '[': 13 | store[k++] = s[i]; 14 | break; 15 | case ')': 16 | if (k < 1 || store[--k] != '(') 17 | goto out; 18 | break; 19 | case '}': 20 | if (k < 1 || store[--k] != '{') 21 | goto out; 22 | break; 23 | case ']': 24 | if (k < 1 || store[--k] != '[') 25 | goto out; 26 | break; 27 | } 28 | } 29 | out: 30 | free(store); 31 | return s[i] == '\0' && k == 0; 32 | } 33 | -------------------------------------------------------------------------------- /201.c: -------------------------------------------------------------------------------- 1 | int rangeBitwiseAnd(int m, int n) 2 | { 3 | while (m < n) 4 | { 5 | n &= n - 1; 6 | } 7 | return n; 8 | } -------------------------------------------------------------------------------- /203.c: -------------------------------------------------------------------------------- 1 | struct ListNode *removeElements(struct ListNode *head, int val) 2 | { 3 | if (head == NULL) 4 | return NULL; 5 | if (head->val == val) 6 | { 7 | return removeElements(head->next, val); 8 | } 9 | else 10 | { 11 | head->next = removeElements(head->next, val); 12 | } 13 | return head; 14 | } 15 | -------------------------------------------------------------------------------- /206.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * struct ListNode *next; 6 | * }; 7 | */ 8 | 9 | struct ListNode *reverseList(struct ListNode *head) 10 | { 11 | struct ListNode *res = NULL; 12 | while (head) 13 | { 14 | struct ListNode *pre_node = head; 15 | head = head->next; 16 | pre_node->next = res; 17 | res = pre_node; 18 | } 19 | return res; 20 | } 21 | -------------------------------------------------------------------------------- /21.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Iterative approach 3 | */ 4 | struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2) 5 | { 6 | struct ListNode *list = NULL; 7 | struct ListNode *tmp = NULL; 8 | 9 | if (!l1) 10 | return l2; 11 | if (!l2) 12 | return l1; 13 | 14 | if (l1 && l2) 15 | { 16 | if (l1->val < l2->val) 17 | { 18 | list = tmp = l1; 19 | l1 = l1->next; 20 | } 21 | else 22 | { 23 | list = tmp = l2; 24 | l2 = l2->next; 25 | } 26 | 27 | while (l1 && l2) 28 | { 29 | if (l1->val < l2->val) 30 | { 31 | tmp->next = l1; 32 | l1 = l1->next; 33 | } 34 | else 35 | { 36 | tmp->next = l2; 37 | l2 = l2->next; 38 | } 39 | tmp = tmp->next; 40 | } 41 | 42 | if (l1) 43 | tmp->next = l1; 44 | if (l2) 45 | tmp->next = l2; 46 | 47 | return list; 48 | } 49 | 50 | return NULL; 51 | } 52 | 53 | /* 54 | * Recursive approach 55 | */ 56 | struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2) 57 | { 58 | if (!l1) 59 | return l2; 60 | if (!l2) 61 | return l1; 62 | if (l1->val < l2->val) 63 | { 64 | l1->next = mergeTwoLists(l1->next, l2); 65 | return l1; 66 | } 67 | else 68 | { 69 | l2->next = mergeTwoLists(l1, l2->next); 70 | return l2; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /215.c: -------------------------------------------------------------------------------- 1 | int *cmpval(const void *a, const void *b) { return *(int *)b - *(int *)a; } 2 | 3 | int findKthLargest(int *nums, int numsSize, int k) 4 | { 5 | qsort(nums, numsSize, sizeof(int), cmpval); 6 | return nums[k - 1]; 7 | } 8 | -------------------------------------------------------------------------------- /217.c: -------------------------------------------------------------------------------- 1 | int numcmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } 2 | 3 | bool containsDuplicate(int *nums, int numsSize) 4 | { 5 | int i; 6 | qsort(nums, numsSize, sizeof(int), numcmp); 7 | for (i = 0; i < numsSize - 1; i++) 8 | { 9 | if (nums[i] == nums[i + 1]) 10 | return 1; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /226.c: -------------------------------------------------------------------------------- 1 | struct TreeNode *invertTree(struct TreeNode *root) 2 | { 3 | struct TreeNode *tmp; 4 | if (root == NULL) 5 | return NULL; 6 | tmp = root->left; 7 | root->left = root->right; 8 | root->right = tmp; 9 | 10 | invertTree(root->left); 11 | invertTree(root->right); 12 | return root; 13 | } 14 | -------------------------------------------------------------------------------- /231.c: -------------------------------------------------------------------------------- 1 | bool isPowerOfTwo(int n) 2 | { 3 | if (!n) 4 | return false; 5 | while (n % 2 == 0) n /= 2; 6 | return n == 1; 7 | } -------------------------------------------------------------------------------- /234.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * struct ListNode *next; 6 | * }; 7 | */ 8 | 9 | struct ListNode *reverse(struct ListNode *head) 10 | { 11 | struct ListNode *res = NULL; 12 | while (head) 13 | { 14 | struct ListNode *pre_node = head; 15 | head = head->next; 16 | pre_node->next = res; 17 | res = pre_node; 18 | } 19 | return res; 20 | } 21 | bool isPalindrome(struct ListNode *head) 22 | { 23 | struct ListNode *slow = head; 24 | struct ListNode *fast = head; 25 | struct ListNode *last; 26 | while (fast && fast->next) 27 | { 28 | slow = slow->next; 29 | fast = fast->next->next; 30 | } 31 | if (fast != NULL) 32 | slow = slow->next; 33 | last = reverse(slow); 34 | while (last) 35 | { 36 | if (head->val != last->val) 37 | return 0; 38 | head = head->next; 39 | last = last->next; 40 | } 41 | return 1; 42 | } 43 | -------------------------------------------------------------------------------- /24.c: -------------------------------------------------------------------------------- 1 | struct ListNode *swapPairs(struct ListNode *head) 2 | { 3 | if (!head || !head->next) 4 | return head; 5 | struct ListNode *tmp = head->next; 6 | head->next = swapPairs(head->next->next); 7 | tmp->next = head; 8 | return tmp; 9 | } 10 | -------------------------------------------------------------------------------- /242.c: -------------------------------------------------------------------------------- 1 | bool isAnagram(char *s, char *t) 2 | { 3 | int n = strlen(s); 4 | int m = strlen(t); 5 | 6 | int cnt_s[1000], cnt_t[1000]; 7 | for (int c = 97; c < 97 + 26; c++) cnt_s[c] = cnt_t[c] = 0; 8 | 9 | for (int i = 0; i < n; i++) cnt_s[s[i]]++; 10 | 11 | for (int i = 0; i < m; i++) cnt_t[t[i]]++; 12 | 13 | for (int c = 97; c < 97 + 26; c++) 14 | if (cnt_s[c] != cnt_t[c]) 15 | return false; 16 | 17 | return true; 18 | } 19 | -------------------------------------------------------------------------------- /26.c: -------------------------------------------------------------------------------- 1 | int removeDuplicates(int *nums, int numsSize) 2 | { 3 | int count = 0, i; 4 | for (i = 1; i < numsSize; i++) 5 | { 6 | if (nums[i] == nums[i - 1]) 7 | count++; 8 | else 9 | nums[i - count] = nums[i]; 10 | } 11 | return numsSize - count; 12 | } 13 | -------------------------------------------------------------------------------- /268.c: -------------------------------------------------------------------------------- 1 | int missingNumber(int *nums, int numsSize) 2 | { 3 | int i, actual_sum = 0, sum = 0; 4 | for (i = 0; i < numsSize; i++) 5 | { 6 | sum = sum + nums[i]; 7 | actual_sum = actual_sum + i; 8 | } 9 | return actual_sum + numsSize - sum; 10 | } 11 | -------------------------------------------------------------------------------- /27.c: -------------------------------------------------------------------------------- 1 | int removeElement(int *nums, int numsSize, int val) 2 | { 3 | int i, start = 0; 4 | for (i = 0; i < numsSize; i++) 5 | { 6 | if (nums[i] != val) 7 | nums[start++] = nums[i]; 8 | } 9 | return start; 10 | } 11 | -------------------------------------------------------------------------------- /278.c: -------------------------------------------------------------------------------- 1 | // Forward declaration of isBadVersion API. 2 | bool isBadVersion(int version); 3 | 4 | int firstBadVersion(int n) 5 | { 6 | int low = 1, high = n; 7 | while (low <= high) 8 | { 9 | int mid = low + (high - low) / 2; 10 | if (isBadVersion(mid)) 11 | { 12 | high = mid - 1; 13 | } 14 | else 15 | { 16 | low = mid + 1; 17 | } 18 | } 19 | return low; 20 | } 21 | -------------------------------------------------------------------------------- /28.c: -------------------------------------------------------------------------------- 1 | /* 2 | * brute force approach 3 | * time complexity: O(mn) 4 | */ 5 | int strStr(char *haystack, char *needle) 6 | { 7 | int i = 0; 8 | int j = 0; 9 | int k = 0; 10 | int hlen = 0; 11 | int nlen = 0; 12 | 13 | if (needle == NULL || *needle == 0) 14 | return 0; 15 | 16 | if (haystack == NULL || *haystack == 0) 17 | return -1; 18 | 19 | hlen = strlen(haystack); 20 | nlen = strlen(needle); 21 | 22 | if (hlen < nlen) 23 | return -1; 24 | 25 | for (i = 0; i <= hlen - nlen; i++) 26 | { 27 | j = 0; 28 | if (haystack[i] != needle[j++]) 29 | continue; 30 | 31 | k = i + 1; 32 | for (; j < nlen; j++) 33 | { 34 | if (haystack[k] != needle[j]) 35 | { 36 | break; 37 | } 38 | else 39 | k++; 40 | } 41 | if (j == nlen) 42 | return i; 43 | } 44 | return -1; 45 | } 46 | 47 | /* ---------------------------------------------------------------------------------------- 48 | */ 49 | 50 | /* 51 | * KMP algorithm 52 | * time complexity: O(m + n) 53 | */ 54 | 55 | /* fills overlap with longest proper prefix which is also suffix for each index 56 | * in needle */ 57 | void fill_overlap(char *needle, int len_needle, int *overlap) 58 | { 59 | int len = 0; 60 | int i = 0; 61 | 62 | overlap[0] = 0; 63 | 64 | for (i = 1; i < len_needle;) 65 | { 66 | if (needle[i] == needle[len]) 67 | { 68 | len++; 69 | overlap[i++] = len; 70 | } 71 | else 72 | { 73 | if (len) 74 | len = overlap[len - 1]; 75 | else 76 | overlap[i++] = 0; 77 | } 78 | } 79 | } 80 | 81 | int strStr(char *haystack, char *needle) 82 | { 83 | int i = 0; /* index for haystack */ 84 | int j = 0; /* index for needle */ 85 | 86 | int len_needle = strlen(needle); 87 | int len_haystack = strlen(haystack); 88 | 89 | if (!len_needle) 90 | return 0; 91 | 92 | int overlap[len_needle]; 93 | 94 | fill_overlap(needle, len_needle, overlap); 95 | 96 | while (i < len_haystack) 97 | { 98 | if (needle[j] == haystack[i]) 99 | { 100 | i++; 101 | j++; 102 | } 103 | 104 | if (j == len_needle) 105 | { 106 | return (i - j); 107 | } 108 | else if (i < len_haystack && needle[j] != haystack[i]) 109 | { 110 | if (j != 0) 111 | j = overlap[j - 1]; 112 | else 113 | i = i + 1; 114 | } 115 | } 116 | return -1; 117 | } 118 | 119 | /* ---------------------------------------------------------------------------------------- 120 | */ 121 | -------------------------------------------------------------------------------- /283.c: -------------------------------------------------------------------------------- 1 | void moveZeroes(int *nums, int numsSize) 2 | { 3 | int i = 0, start = 0; 4 | 5 | for (i = 0; i < numsSize; i++) 6 | { 7 | if (nums[i]) 8 | nums[start++] = nums[i]; 9 | } 10 | 11 | for (start; start < numsSize; start++) 12 | { 13 | nums[start] = 0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /287.c: -------------------------------------------------------------------------------- 1 | int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; } 2 | int findDuplicate(int *nums, int numsSize) 3 | { 4 | int i; 5 | qsort(nums, numsSize, sizeof(int), cmpval); 6 | for (i = 0; i < numsSize - 1; i++) 7 | { 8 | if (nums[i] == nums[i + 1]) 9 | return nums[i]; 10 | } 11 | return nums[i]; 12 | } 13 | -------------------------------------------------------------------------------- /29.c: -------------------------------------------------------------------------------- 1 | int divide(int dividend, int divisor) 2 | { 3 | int sign = 1; 4 | long int output = 0; 5 | if (dividend < 0) 6 | { 7 | sign *= -1; 8 | } 9 | else 10 | { 11 | dividend *= -1; 12 | } 13 | if (divisor < 0) 14 | { 15 | sign *= -1; 16 | } 17 | else 18 | { 19 | divisor *= -1; 20 | } 21 | while (dividend <= divisor) 22 | { 23 | long int tmp = 0; 24 | long int div = divisor; 25 | while (dividend <= div) 26 | { 27 | tmp += (tmp + 1); 28 | dividend -= div; 29 | div += div; 30 | } 31 | if (output >= INT_MAX) 32 | { 33 | if (sign == -1) 34 | { 35 | return INT_MIN; 36 | } 37 | else 38 | { 39 | return INT_MAX; 40 | } 41 | } 42 | output += tmp; 43 | } 44 | 45 | return output * sign; 46 | } 47 | -------------------------------------------------------------------------------- /3.c: -------------------------------------------------------------------------------- 1 | int lengthOfLongestSubstring(char *str) 2 | { 3 | int n = strlen(str); 4 | 5 | if (!n) 6 | return 0; 7 | 8 | int L_len = 1; // lenght of longest substring 9 | int C_len = 1; // lenght of current substring 10 | 11 | int P_ind, i; // P_ind for previous index 12 | int visited[256]; // visited will keep track of visiting char for the last 13 | // instance. since there are 256 ASCII char, its size is 14 | // limited to that value. 15 | memset(visited, -1, sizeof(int) * 256); 16 | visited[str[0]] = 17 | 0; // the index of that char will tell us that when it was visited. 18 | for (i = 1; i < n; i++) 19 | { 20 | P_ind = visited[str[i]]; 21 | if (P_ind == -1 || i - C_len > P_ind) 22 | C_len++; // if the current char was not visited earlier, or it is 23 | // not the part of current substring 24 | else 25 | { // otherwise, we need to change the current/longest substring length 26 | if (C_len > L_len) 27 | L_len = C_len; 28 | C_len = i - P_ind; 29 | } 30 | visited[str[i]] = i; 31 | } 32 | if (C_len > L_len) 33 | L_len = C_len; 34 | return L_len; 35 | } 36 | /* Brute force */ 37 | int lengthOfLongestSubstring(char *s) 38 | { 39 | int cur_max = 0, max = 0; 40 | int counter[255]; 41 | int end = 0; 42 | 43 | memset(counter, 0, sizeof(int) * 255); 44 | while (end < strlen(s)) 45 | { 46 | if (counter[s[end]] == 0) 47 | { 48 | counter[s[end]]++; 49 | end++; 50 | cur_max++; 51 | } 52 | else 53 | { 54 | char c = s[end]; 55 | memset(counter, 0, 255 * sizeof(int)); 56 | if (cur_max >= max) 57 | max = cur_max; 58 | cur_max = 0; 59 | while (s[end - 1] != c) end--; 60 | } 61 | } 62 | if (cur_max >= max) 63 | max = cur_max; 64 | return max; 65 | } 66 | -------------------------------------------------------------------------------- /344.c: -------------------------------------------------------------------------------- 1 | void reverseString(char *s, int sSize) 2 | { 3 | int last = sSize - 1, i; 4 | for (i = 0; i < last; i++) 5 | { 6 | char tmp = s[i]; 7 | s[i] = s[last]; 8 | s[last] = tmp; 9 | last--; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /35.c: -------------------------------------------------------------------------------- 1 | int searchInsert(int *nums, int numsSize, int target) 2 | { 3 | int low = 0, high = numsSize - 1, mid; 4 | while (low <= high) 5 | { 6 | mid = low + (high - low) / 2; 7 | if (target > nums[mid]) 8 | low = mid + 1; 9 | else if (target < nums[mid]) 10 | high = mid - 1; 11 | else 12 | return mid; 13 | } 14 | return low; 15 | } 16 | 17 | /* Recursive version */ 18 | int searchInsert(int *nums, int numsSize, int target) 19 | { 20 | int idx = numsSize - 1; 21 | if (numsSize > 0) 22 | { 23 | if (target > nums[idx]) 24 | { 25 | return numsSize; 26 | } 27 | return searchInsert(nums, numsSize - 1, target); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /367.c: -------------------------------------------------------------------------------- 1 | bool isPerfectSquare(int num) 2 | { 3 | for (long i = 1; i * i <= num; i++) 4 | if (i * i == num) 5 | return true; 6 | return false; 7 | } 8 | -------------------------------------------------------------------------------- /38.c: -------------------------------------------------------------------------------- 1 | char *countAndSay(int n) 2 | { 3 | // Calculating the length of array 4 | double result = 1.0; 5 | for (int i = 0; i < n - 1; i++) 6 | { 7 | result *= 1.4; 8 | } 9 | 10 | int k, j, count, convert = (int)result; 11 | 12 | // Creating array with the length calculated above 13 | char *arr = malloc(convert + 4); 14 | arr[0] = '1'; 15 | arr[1] = '\0'; 16 | 17 | for (int i = 2, length; i <= n; i++) 18 | { 19 | length = strlen(arr); 20 | char newArr[length * 2]; 21 | strcpy(newArr, arr); 22 | 23 | k = 0; 24 | j = 0; 25 | count = 1; 26 | 27 | while (newArr[j] != '\0') 28 | { 29 | if (newArr[j] == newArr[j + 1]) 30 | { 31 | count++; 32 | j++; 33 | } 34 | else 35 | { 36 | arr[k] = (48 + count); 37 | arr[k + 1] = newArr[j]; 38 | arr[k + 2] = '\0'; 39 | j++; 40 | k += 2; 41 | count = 1; 42 | } 43 | } 44 | } 45 | 46 | return arr; 47 | } 48 | -------------------------------------------------------------------------------- /387.c: -------------------------------------------------------------------------------- 1 | int firstUniqChar(char *s) 2 | { 3 | int *arr = calloc(256, sizeof(int)); 4 | int i; 5 | for (i = 0; i < strlen(s); i++) arr[s[i]] = arr[s[i]] + 1; 6 | for (i = 0; i < strlen(s); i++) 7 | { 8 | if (arr[s[i]] == 1) 9 | return i; 10 | } 11 | return -1; 12 | } 13 | -------------------------------------------------------------------------------- /389.c: -------------------------------------------------------------------------------- 1 | char findTheDifference(char *s, char *t) 2 | { 3 | int sum1 = 0, sum2 = 0; 4 | int i; 5 | for (i = 0; i < strlen(s); i++) sum1 += s[i]; 6 | for (i = 0; i < strlen(t); i++) sum2 += t[i]; 7 | return (char)(sum2 - sum1); 8 | } 9 | -------------------------------------------------------------------------------- /4.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | double findMedianSortedArrays(int *nums1, int nums1Size, int *nums2, 4 | int nums2Size) 5 | { 6 | int index1 = 0; 7 | int index2 = 0; 8 | int v[nums1Size + nums2Size]; 9 | int v_index = 0; 10 | 11 | while (index1 < nums1Size && index2 < nums2Size) 12 | { 13 | if (nums1[index1] <= nums2[index2]) 14 | { 15 | v[v_index++] = nums1[index1++]; 16 | } 17 | else 18 | { 19 | v[v_index++] = nums2[index2++]; 20 | } 21 | } 22 | if (index1 < nums1Size) 23 | { 24 | while (index1 < nums1Size) 25 | { 26 | v[v_index++] = nums1[index1++]; 27 | } 28 | } 29 | if (index2 < nums2Size) 30 | { 31 | while (index2 < nums2Size) 32 | { 33 | v[v_index++] = nums2[index2++]; 34 | } 35 | } 36 | if (v_index == 1) 37 | { 38 | return v[0]; 39 | } 40 | if (v_index % 2 == 0) 41 | { 42 | double n1, n2; 43 | n1 = v[v_index / 2]; 44 | n2 = v[(v_index / 2) - 1]; 45 | return (n1 + n2) / 2; 46 | } 47 | int new_index = (int)v_index / 2; 48 | int i = 0; 49 | return v[new_index]; 50 | } 51 | -------------------------------------------------------------------------------- /404.c: -------------------------------------------------------------------------------- 1 | bool isleaf(struct TreeNode *root) 2 | { 3 | return root->left == NULL && root->right == NULL; 4 | } 5 | 6 | int sumOfLeftLeaves(struct TreeNode *root) 7 | { 8 | if (root == NULL) 9 | return 0; 10 | if (root->left) 11 | { 12 | if (isleaf(root->left)) 13 | return root->left->val + sumOfLeftLeaves(root->right); 14 | } 15 | return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); 16 | } 17 | -------------------------------------------------------------------------------- /442.c: -------------------------------------------------------------------------------- 1 | int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; } 2 | 3 | int *findDuplicates(int *nums, int numsSize, int *returnSize) 4 | { 5 | int i; 6 | qsort(nums, numsSize, sizeof(int), cmpval); 7 | int *retArr = malloc(numsSize * sizeof(int)); 8 | *returnSize = 0; 9 | for (i = 0; i < numsSize - 1;) 10 | { 11 | if (nums[i] == nums[i + 1]) 12 | { 13 | retArr[*returnSize] = nums[i]; 14 | *returnSize = *returnSize + 1; 15 | i = i + 2; 16 | } 17 | else 18 | { 19 | i = i + 1; 20 | } 21 | } 22 | return retArr; 23 | } 24 | -------------------------------------------------------------------------------- /461.c: -------------------------------------------------------------------------------- 1 | int hammingDistance(int x, int y) 2 | { 3 | int difference = 4 | x ^ y; // The XOR operator generates the bitwise difference in the 5 | // binary representation of two numbers If bit in ith position 6 | // of both numbers is same, bit in difference is 0, otherwise 1 7 | int TotalBits = sizeof(difference) * 8; // total number of bits 8 | int i, distance = 0; 9 | for (i = 0; i < TotalBits; i++) 10 | { 11 | if (difference & 12 | (UINT32_C(1) 13 | << i)) // if the bit on the ith position of 32 bit input is 1, 14 | // then proceed Further note the use of UINT32_C to convert 15 | // 1 to unsigned 32 bit int, as just 1 is treated as int 16 | // which cannot be shifted left more than 30 times 17 | distance += 1; 18 | } 19 | return distance; 20 | } -------------------------------------------------------------------------------- /476.c: -------------------------------------------------------------------------------- 1 | int findComplement(int num) 2 | { 3 | int TotalBits = 0; 4 | int temp = num; 5 | while (temp) 6 | { // To find position of MSB in given num. Since num is represented as a 7 | // standard size in memory, we cannot rely on size for that information. 8 | TotalBits++; // increment TotalBits till temp becomes 0 9 | temp >>= 1; // shift temp right by 1 bit every iteration; temp loses 1 10 | // bit to underflow every iteration till it becomes 0 11 | } 12 | int i, 13 | flipNumber = 1; // Eg: 1's complement of 101(binary) can be found as 14 | // 101^111 (XOR with 111 flips all bits that are 1 to 0 15 | // and flips 0 to 1) 16 | for (i = 1; i < TotalBits; i++) 17 | { 18 | flipNumber += UINT32_C(1) 19 | << i; // Note the use of unsigned int to facilitate left 20 | // shift more than 31 times, if needed 21 | } 22 | num = num ^ flipNumber; 23 | return num; 24 | } -------------------------------------------------------------------------------- /509.c: -------------------------------------------------------------------------------- 1 | int fib(int N) 2 | { 3 | if (N == 0) 4 | return 0; 5 | if (N == 1) 6 | return 1; 7 | return fib(N - 1) + fib(N - 2); 8 | } 9 | -------------------------------------------------------------------------------- /520.c: -------------------------------------------------------------------------------- 1 | bool detectCapitalUse(char *word) 2 | { 3 | int len = strlen(word); 4 | if (len == 1) 5 | return 1; 6 | int countUpper = 0, i; 7 | for (i = 0; i < len; i++) 8 | { 9 | if (isupper(word[i])) 10 | countUpper++; 11 | } 12 | /* All lower case */ 13 | if (countUpper == 0) 14 | return 1; 15 | /* 1st character is upper, and the rest is lower case */ 16 | if (countUpper == 1 && isupper(word[0])) 17 | return 1; 18 | /* Check all character is upper case? */ 19 | else 20 | return countUpper == len; 21 | } 22 | 23 | /* Another way */ 24 | bool isAllUpper(char *word) 25 | { 26 | int len = strlen(word); 27 | for (int i = 0; i < len; i++) 28 | { 29 | if (islower(word[i])) 30 | return 0; 31 | } 32 | return 1; 33 | } 34 | bool detectCapitalUse(char *word) 35 | { 36 | int len = strlen(word); 37 | for (int i = 1; i < len; i++) 38 | { 39 | if (isupper(word[i]) && !isAllUpper(word)) 40 | return 0; 41 | } 42 | return 1; 43 | } 44 | -------------------------------------------------------------------------------- /53.c: -------------------------------------------------------------------------------- 1 | 2 | int maxcmp(int a, int b) { return a >= b ? a : b; } 3 | 4 | int maxSubArray(int *nums, int numsSize) 5 | { 6 | int maxSoFar = nums[0], maxEndingHere = nums[0]; 7 | for (int i = 1; i < numsSize; i++) 8 | { 9 | maxEndingHere = maxcmp(maxEndingHere + nums[i], nums[i]); 10 | maxSoFar = maxcmp(maxSoFar, maxEndingHere); 11 | } 12 | return maxSoFar; 13 | } 14 | -------------------------------------------------------------------------------- /561.c: -------------------------------------------------------------------------------- 1 | int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; } 2 | int arrayPairSum(int *nums, int numsSize) 3 | { 4 | int sum = 0, i; 5 | qsort(nums, numsSize, sizeof(int), cmpval); 6 | for (i = 0; i < numsSize; i = i + 2) sum = sum + nums[i]; 7 | return sum; 8 | } 9 | -------------------------------------------------------------------------------- /617.c: -------------------------------------------------------------------------------- 1 | struct TreeNode *newNode(int item) 2 | { 3 | struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode)); 4 | node->val = item; 5 | node->left = node->right = NULL; 6 | return node; 7 | } 8 | 9 | struct TreeNode *mergeTrees(struct TreeNode *t1, struct TreeNode *t2) 10 | { 11 | if (t1 == NULL && t2 == NULL) 12 | return NULL; 13 | int item = (t1 == NULL ? 0 : t1->val) + (t2 == NULL ? 0 : t2->val); 14 | struct TreeNode *node = newNode(item); 15 | node->left = 16 | mergeTrees(t1 == NULL ? NULL : t1->left, t2 == NULL ? NULL : t2->left); 17 | node->right = mergeTrees(t1 == NULL ? NULL : t1->right, 18 | t2 == NULL ? NULL : t2->right); 19 | return node; 20 | } 21 | -------------------------------------------------------------------------------- /647.c: -------------------------------------------------------------------------------- 1 | 2 | /* Author : Saurav Dubey */ 3 | 4 | int countSubstrings(char *s) 5 | { 6 | int len = strlen(s); 7 | int i; 8 | int count = 0; 9 | for (i = 0; i < len; i++) 10 | { 11 | // cases handled for both odd and even lenghted Palindrome 12 | 13 | count += countPalin(s, i, i, len); 14 | if (i != len - 1) 15 | count += countPalin(s, i, i + 1, len); 16 | } 17 | return count; 18 | } 19 | int countPalin(char *s, int head, int tail, int len) 20 | { 21 | int ret = (s[head] == s[tail]) ? 1 : 0; 22 | if (ret && head - 1 >= 0 && tail + 1 < len) 23 | ret += countPalin(s, head - 1, tail + 1, len); 24 | return ret; 25 | } 26 | -------------------------------------------------------------------------------- /66.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Note: The returned array must be malloced, assume caller calls free(). 3 | */ 4 | int *plusOne(int *digits, int digitsSize, int *returnSize) 5 | { 6 | for (int i = digitsSize - 1; i >= 0; i--) 7 | { 8 | if (digits[i] < 9) 9 | { 10 | digits[i]++; 11 | *returnSize = digitsSize; 12 | return digits; 13 | } 14 | else 15 | { 16 | digits[i] = 0; 17 | } 18 | } 19 | 20 | int *newdigit = (int *)malloc((digitsSize + 1) * sizeof(int)); 21 | newdigit[0] = 1; 22 | for (int i = 1; i < (digitsSize + 1); i++) 23 | { 24 | newdigit[i] = digits[i - 1]; 25 | } 26 | *returnSize = digitsSize + 1; 27 | return newdigit; 28 | } -------------------------------------------------------------------------------- /674.c: -------------------------------------------------------------------------------- 1 | int findLengthOfLCIS(int *nums, int numsSize) 2 | { 3 | int maxval = 1, i, count = 1; 4 | if (numsSize == 0) 5 | return 0; 6 | for (i = 1; i < numsSize; i++) 7 | { 8 | if (nums[i] > nums[i - 1]) 9 | { 10 | count++; 11 | if (count >= maxval) 12 | maxval = count; 13 | } 14 | else 15 | { 16 | count = 1; 17 | } 18 | } 19 | return maxval; 20 | } 21 | -------------------------------------------------------------------------------- /7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int reverse(int x) 4 | { 5 | int rev = 0; 6 | while (x != 0) 7 | { 8 | int pop = x % 10; 9 | x /= 10; 10 | if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && pop > 7)) 11 | return 0; 12 | if (rev < INT_MIN / 10 || (rev == INT_MIN / 10 && pop < -8)) 13 | return 0; 14 | rev = rev * 10 + pop; 15 | } 16 | return rev; 17 | } 18 | -------------------------------------------------------------------------------- /700.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * struct TreeNode *left; 6 | * struct TreeNode *right; 7 | * }; 8 | */ 9 | 10 | struct TreeNode *searchBST(struct TreeNode *root, int val) 11 | { 12 | if (!root) 13 | return NULL; 14 | 15 | if (root->val == val) 16 | { 17 | return root; 18 | } 19 | else if (root->val > val) 20 | { 21 | return searchBST(root->left, val); 22 | } 23 | else 24 | { 25 | return searchBST(root->right, val); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /701.c: -------------------------------------------------------------------------------- 1 | struct TreeNode *insertIntoBST(struct TreeNode *root, int val) 2 | { 3 | if (root == NULL) 4 | { 5 | struct TreeNode *new_val = malloc(sizeof(struct TreeNode)); 6 | new_val->val = val; 7 | new_val->left = new_val->right = NULL; 8 | return new_val; 9 | } 10 | else 11 | { 12 | if (root->val >= val) 13 | root->left = insertIntoBST(root->left, val); 14 | else 15 | root->right = insertIntoBST(root->right, val); 16 | } 17 | return root; 18 | } 19 | -------------------------------------------------------------------------------- /704.c: -------------------------------------------------------------------------------- 1 | int search(int *nums, int numsSize, int target) 2 | { 3 | int low = 0, high = numsSize - 1; 4 | while (low <= high) 5 | { 6 | int mid = low + (high - low) / 2; 7 | if (target > nums[mid]) 8 | { 9 | low = mid + 1; 10 | } 11 | else if (target < nums[mid]) 12 | { 13 | high = mid - 1; 14 | } 15 | else 16 | { 17 | return mid; 18 | } 19 | } 20 | return -1; 21 | } 22 | 23 | /* Another solution: Using bsearch() */ 24 | int cmpint(const void *a, const void *b) { return *(int *)a - *(int *)b; } 25 | 26 | int search(int *nums, int numsSize, int target) 27 | { 28 | int *ret = bsearch(&target, nums, numsSize, sizeof(int), cmpint); 29 | if (ret) 30 | return (ret - nums); 31 | else 32 | return -1; 33 | } 34 | -------------------------------------------------------------------------------- /709.c: -------------------------------------------------------------------------------- 1 | char *toLowerCase(char *str) 2 | { 3 | for (int i = 0; i < strlen(str); i++) str[i] = tolower(str[i]); 4 | return str; 5 | } 6 | -------------------------------------------------------------------------------- /771.c: -------------------------------------------------------------------------------- 1 | // for strlen() 2 | #include 3 | 4 | int numJewelsInStones(char *j, char *s) 5 | { 6 | // as strlen is O(n), store it once rather than using it in for loop 7 | int cnt[500], lens = strlen(s), lenj = strlen(j), sol = 0; 8 | memset(cnt, 0, sizeof(cnt)); 9 | 10 | // lookup to know which character occurs in j 11 | for (int i = 0; i < lenj; i++) cnt[j[i]]++; 12 | 13 | // count the characters in s 14 | for (int i = 0; i < lens; i++) sol += cnt[s[i]]; 15 | 16 | return sol; 17 | } 18 | -------------------------------------------------------------------------------- /8.c: -------------------------------------------------------------------------------- 1 | int myAtoi(char *str) 2 | { 3 | int minusFlag = 0; 4 | int length = strlen(str); 5 | long int result = 0; 6 | char numberBuffer[11]; 7 | int counter = 0; 8 | while (str[counter] == ' ') 9 | { 10 | counter++; 11 | } 12 | str = &str[counter]; 13 | counter = 0; 14 | 15 | for (int i = 0; i < length; i++) 16 | { 17 | if (i == 0) 18 | { 19 | if (str[0] == '-') 20 | { 21 | minusFlag = 1; 22 | i++; 23 | } 24 | else if (str[0] == '+') 25 | { 26 | i++; 27 | } 28 | } 29 | if (counter > 10) 30 | { 31 | if (minusFlag) 32 | { 33 | return __INT_MAX__ * -1 - 1; 34 | } 35 | else 36 | { 37 | return __INT_MAX__; 38 | } 39 | } 40 | 41 | if (str[i] < '0' || str[i] > '9') 42 | { 43 | break; 44 | } 45 | if (counter == 0 && str[i] == '0') 46 | { 47 | continue; 48 | } 49 | 50 | numberBuffer[counter] = str[i]; 51 | counter++; 52 | } 53 | 54 | int i = 0; 55 | while (counter > 0) 56 | { 57 | if (minusFlag) 58 | { 59 | result -= (numberBuffer[i] - '0') * pow(10.0, counter - 1); 60 | } 61 | else 62 | { 63 | result += (numberBuffer[i] - '0') * pow(10.0, counter - 1); 64 | } 65 | i++; 66 | counter--; 67 | } 68 | 69 | if (result > __INT_MAX__) 70 | { 71 | return __INT_MAX__; 72 | } 73 | else if (result < __INT_MAX__ * -1 - 1) 74 | { 75 | return __INT_MAX__ * -1 - 1; 76 | } 77 | return result; 78 | } 79 | -------------------------------------------------------------------------------- /82.c: -------------------------------------------------------------------------------- 1 | struct ListNode *deleteDuplicates(struct ListNode *head) 2 | { 3 | if (head == NULL) 4 | return NULL; 5 | 6 | if (head->next && head->val == head->next->val) 7 | { 8 | /* Remove all duplicate numbers */ 9 | while (head->next && head->val == head->next->val) 10 | { 11 | head = head->next; 12 | } 13 | return deleteDuplicates(head->next); 14 | } 15 | else 16 | { 17 | head->next = deleteDuplicates(head->next); 18 | } 19 | return head; 20 | } 21 | -------------------------------------------------------------------------------- /83.c: -------------------------------------------------------------------------------- 1 | 2 | struct ListNode *deleteDuplicates(struct ListNode *head) 3 | { 4 | struct ListNode *cur = head; 5 | while (cur && cur->next) 6 | { 7 | if (cur->val == cur->next->val) 8 | cur->next = cur->next->next; 9 | else 10 | cur = cur->next; 11 | } 12 | return head; 13 | } 14 | -------------------------------------------------------------------------------- /852.c: -------------------------------------------------------------------------------- 1 | int peakIndexInMountainArray(int *A, int ASize) 2 | { 3 | int low = 1, high = ASize; 4 | while (low <= high) 5 | { 6 | int mid = low + (high - low) / 2; 7 | if (A[mid - 1] < A[mid] && A[mid] > A[mid + 1]) 8 | return mid; 9 | else if (A[mid - 1] < A[mid] && A[mid] < A[mid + 1]) 10 | low = mid + 1; 11 | else 12 | high = mid - 1; 13 | } 14 | return -1; 15 | } 16 | -------------------------------------------------------------------------------- /876.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * struct ListNode *next; 6 | * }; 7 | */ 8 | 9 | struct ListNode *middleNode(struct ListNode *head) 10 | { 11 | struct ListNode *fast, *slow; 12 | fast = slow = head; 13 | while (fast && fast->next) 14 | { 15 | slow = slow->next; 16 | fast = fast->next->next; 17 | } 18 | return slow; 19 | } 20 | -------------------------------------------------------------------------------- /9.c: -------------------------------------------------------------------------------- 1 | bool isPalindrome(int x) 2 | { 3 | if (x < 0 || (x % 10 == 0 && x != 0)) 4 | { 5 | return false; 6 | } 7 | 8 | int revertedNumber = 0; 9 | while (x > revertedNumber) 10 | { 11 | revertedNumber = revertedNumber * 10 + x % 10; 12 | x /= 10; 13 | } 14 | 15 | return x == revertedNumber || x == revertedNumber / 10; 16 | } 17 | -------------------------------------------------------------------------------- /905.c: -------------------------------------------------------------------------------- 1 | /** 2 | * 905. Sort Array By Parity 3 | * Given an array A of non-negative integers, return an array consisting of 4 | * all the even elements of A, followed by all the odd elements of A. 5 | * You may return any answer array that satisfies this condition. 6 | * Example 1: 7 | * Input: [3,1,2,4] 8 | * Output: [2,4,3,1] 9 | * The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. 10 | * 11 | * Note: The returned array must be malloced, assume caller calls free(). 12 | */ 13 | int *sortArrayByParity(int *A, int ASize, int *returnSize) 14 | { 15 | int *retArr = malloc(ASize * sizeof(int)); 16 | int oddIndex = ASize - 1; 17 | int evenIndex = 0; 18 | *returnSize = ASize; 19 | for (int i = 0; i < ASize; i++) 20 | { 21 | if (A[i] % 2 == 0) 22 | { 23 | retArr[evenIndex] = A[i]; 24 | evenIndex++; 25 | } 26 | else 27 | { 28 | retArr[oddIndex] = A[i]; 29 | oddIndex--; 30 | } 31 | } 32 | 33 | return retArr; 34 | } 35 | -------------------------------------------------------------------------------- /917.c: -------------------------------------------------------------------------------- 1 | char *reverseOnlyLetters(char *S) 2 | { 3 | int last = strlen(S) - 1, i; 4 | for (i = 0; i < last;) 5 | { 6 | if (!isalpha(S[i])) 7 | { 8 | i++; 9 | continue; 10 | } 11 | if (!isalpha(S[last])) 12 | { 13 | last--; 14 | continue; 15 | } 16 | char tmp = S[i]; 17 | S[i] = S[last]; 18 | S[last] = tmp; 19 | i++; 20 | last--; 21 | } 22 | return S; 23 | } 24 | -------------------------------------------------------------------------------- /938.c: -------------------------------------------------------------------------------- 1 | int rangeSumBST(struct TreeNode *root, int L, int R) 2 | { 3 | if (root == NULL) 4 | { 5 | return 0; 6 | } 7 | else if (root->val >= L && root->val <= R) 8 | { 9 | return root->val + rangeSumBST(root->left, L, R) + 10 | rangeSumBST(root->right, L, R); 11 | } 12 | else 13 | { 14 | return rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /94.c: -------------------------------------------------------------------------------- 1 | void processTraversal(struct TreeNode *root, int *res, int *size) 2 | { 3 | if (!root) 4 | return; 5 | processTraversal(root->left, res, size); 6 | res[*size] = root->val; 7 | *size = *size + 1; 8 | processTraversal(root->right, res, size); 9 | } 10 | 11 | int *inorderTraversal(struct TreeNode *root, int *returnSize) 12 | { 13 | int *res = malloc(256 * sizeof(int)); 14 | *returnSize = 0; 15 | processTraversal(root, res, returnSize); 16 | return res; 17 | } 18 | -------------------------------------------------------------------------------- /965.c: -------------------------------------------------------------------------------- 1 | bool isUnivalTree(struct TreeNode *root) 2 | { 3 | if (root == NULL) 4 | return 1; 5 | if (root->left) 6 | { 7 | if (root->left->val != root->val) 8 | return 0; 9 | } 10 | if (root->right) 11 | { 12 | if (root->right->val != root->val) 13 | return 0; 14 | } 15 | return isUnivalTree(root->left) && isUnivalTree(root->right); 16 | } 17 | -------------------------------------------------------------------------------- /977.c: -------------------------------------------------------------------------------- 1 | /* 1st way: Using 2 pointers */ 2 | int *sortedSquares(int *A, int ASize, int *returnSize) 3 | { 4 | int i, start = 0, end = ASize - 1; 5 | int *res = malloc(ASize * sizeof(int)); 6 | *returnSize = ASize; 7 | for (i = ASize - 1; i >= 0; i--) 8 | { 9 | if (abs(A[start]) > A[end]) 10 | { 11 | res[i] = A[start] * A[start]; 12 | start++; 13 | } 14 | else 15 | { 16 | res[i] = A[end] * A[end]; 17 | end--; 18 | } 19 | } 20 | return res; 21 | } 22 | 23 | /* 2nd way: Using qsort */ 24 | int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; } 25 | 26 | int *sortedSquares(int *A, int ASize, int *returnSize) 27 | { 28 | int *res = malloc(ASize * sizeof(int)); 29 | for (int i = 0; i < ASize; i++) res[i] = A[i] * A[i]; 30 | *returnSize = ASize; 31 | qsort(res, ASize, sizeof(int), cmpval); 32 | return res; 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode-C 2 | Some random CP solutions of Leetcode in C 3 | --------------------------------------------------------------------------------