Skip to content

Commit 2be5ee9

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 034d951 commit 2be5ee9

File tree

17 files changed

+79
-63
lines changed

17 files changed

+79
-63
lines changed

0015_three_sum/three_sum.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
45
static int compare(const void *a, const void *b)
56
{
67
return *(int *) a - *(int *) b;
@@ -30,7 +31,7 @@ static void two_sum(int *nums, int low, int high, int target, int **results, int
3031
** Return an array of arrays of size *returnSize.
3132
** Note: The returned array must be malloced, assume caller calls free().
3233
**/
33-
static int** threeSum(int* nums, int numsSize, int* returnSize)
34+
int** threeSum(int* nums, int numsSize, int* returnSize)
3435
{
3536
if (numsSize < 3) {
3637
return NULL;

0018_four_sum/four_sum.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
56
static int compare(const void *a, const void *b)
67
{
78
return *(int *) a - *(int *) b;
89
}
910

1011
static void k_sum(int *nums, int low, int high, int target, int total, int k,
11-
int *stack, int len, int **results, int *count, int *columnSizes)
12+
int *stack, int len, int **results, int *count, int *col_sizes)
1213
{
1314
int i;
1415
if (k == 2) {
@@ -23,7 +24,7 @@ static void k_sum(int *nums, int low, int high, int target, int total, int k,
2324
stack[len++] = nums[high];
2425
results[*count] = malloc(total * sizeof(int));
2526
memcpy(results[*count], stack, total * sizeof(int));
26-
columnSizes[*count] = total;
27+
col_sizes[*count] = total;
2728
(*count)++;
2829
len -= 2;
2930
while (++low < high && nums[low] == nums[low - 1]) {}
@@ -34,19 +35,19 @@ static void k_sum(int *nums, int low, int high, int target, int total, int k,
3435
/* k > 2 */
3536
for (i = low; i <= high - k + 1; i++) {
3637
if (i > low && nums[i] == nums[i - 1]) continue;
37-
stack[len++] = nums[i];
38-
k_sum(nums, i + 1, high, target - nums[i], 4, k - 1, stack, len, results, count, columnSizes);
39-
len--;
38+
stack[len] = nums[i];
39+
k_sum(nums, i + 1, high, target - nums[i], 4, k - 1, stack, len + 1, results, count, col_sizes);
4040
}
4141
}
4242
}
4343

4444
/**
4545
* Return an array of arrays of size *returnSize.
4646
* The sizes of the arrays are returned as *returnColumnSizes array.
47-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
47+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
4848
*/
49-
int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes) {
49+
int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes)
50+
{
5051
*returnSize = 0;
5152
int i, j, capacity = 50000;
5253
int **results = malloc(capacity * sizeof(int *));
@@ -62,11 +63,11 @@ int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** return
6263

6364
int main(void)
6465
{
65-
int i, count;
66+
int i, count, target = 11, *col_sizes;
6667
//int nums[] = { 1, 0, -1, 0, -2, 2 };
6768
//int nums[] = { -3, -2, -1, 0, 0, 1, 2, 3 };
6869
int nums[] = { 0, 1, 5, 0, 1, 5, 5, -4 };
69-
int **quadruplets = fourSum(nums, sizeof(nums) / sizeof(*nums), 11, &count);
70+
int **quadruplets = fourSum(nums, sizeof(nums) / sizeof(*nums), target, &count, &col_sizes);
7071
for (i = 0; i < count; i++) {
7172
printf("%d %d %d %d\n", quadruplets[i][0], quadruplets[i][1], quadruplets[i][2], quadruplets[i][3]);
7273
}

0039_combination_sum/combination_sum.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ static void dfs(int *nums, int size, int start, int target, int *stack,
2828
** The sizes of the arrays are returned as *returnColumnSizes array.
2929
** Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
3030
**/
31-
static int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int **returnColumnSizes)
31+
int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int **returnColumnSizes)
3232
{
3333
int cap = 200;
34-
int *stack = malloc(candidatesSize * sizeof(int));
34+
int *stack = malloc(cap * sizeof(int));
3535
int **results = malloc(cap * sizeof(int *));
3636
*returnColumnSizes = malloc(cap * sizeof(int));
3737
*returnSize = 0;

0040_combination_sum_ii/combination_sum.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ static int compare(const void *a, const void *b)
1111
}
1212

1313
static void dfs(int *nums, int size, int start, int target, int *solution,
14-
int len, int **results, int *count, int *column_sizes)
14+
int len, int **results, int *count, int *col_sizes)
1515
{
1616
int i;
1717
if (target < 0) {
1818
return;
1919
} else if (target == 0) {
2020
results[*count] = malloc(len * sizeof(int));
2121
memcpy(results[*count], solution, len * sizeof(int));
22-
column_sizes[*count] = len;
22+
col_sizes[*count] = len;
2323
(*count)++;
2424
} else {
2525
int last = INT_MIN;
@@ -28,7 +28,7 @@ static void dfs(int *nums, int size, int start, int target, int *solution,
2828
/* No duplicate combinations in the same level position */
2929
solution[len] = nums[i];
3030
/* i + 1 limits the candidate range in next levels */
31-
dfs(nums, size, i + 1, target - nums[i], solution, len + 1, results, count, column_sizes);
31+
dfs(nums, size, i + 1, target - nums[i], solution, len + 1, results, count, col_sizes);
3232
}
3333
last = nums[i];
3434
}
@@ -40,7 +40,7 @@ static void dfs(int *nums, int size, int start, int target, int *solution,
4040
** The sizes of the arrays are returned as *returnColumnSizes array.
4141
** Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
4242
**/
43-
static int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes)
43+
int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes)
4444
{
4545
qsort(candidates, candidatesSize, sizeof(int), compare);
4646

0046_permutations/permutations.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ static void dfs(int *nums, int size, bool *used, int *stack,
5858
/**
5959
* Return an array of arrays of size *returnSize.
6060
* The sizes of the arrays are returned as *returnColumnSizes array.
61-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
61+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
6262
*/
63-
static int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes)
63+
int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes)
6464
{
6565
int count = 0, cap = 5000;
6666
int **results = malloc(cap * sizeof(int *));

0049_group_anagrams/anagrams.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static inline int BKDRHash(char *s, size_t size)
2929
** The sizes of the arrays are returned as *returnColumnSizes array.
3030
** Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
3131
**/
32-
static char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** returnColumnSizes)
32+
char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** returnColumnSizes)
3333
{
3434
int i, j, count = 0;
3535
int hash_size = strsSize;
@@ -72,10 +72,10 @@ static char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** r
7272

7373
int main(int argc, char **argv)
7474
{
75-
int *column_sizes, count = 0, i, j;
76-
char ***lists = groupAnagrams(argv + 1, argc - 1, &count, &column_sizes);
75+
int *col_sizes, count = 0, i, j;
76+
char ***lists = groupAnagrams(argv + 1, argc - 1, &count, &col_sizes);
7777
for (i = 0; i < count; i++) {
78-
for (j = 0; j < column_sizes[i]; j++) {
78+
for (j = 0; j < col_sizes[i]; j++) {
7979
printf("%s ", lists[i][j]);
8080
}
8181
printf("\n");

0049_group_anagrams/anagrams.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class Solution {
99
unordered_map<string, vector<string>> ht;
1010
for (const auto& str : strs) {
1111
int counts[26] = { 0 };
12-
for (const auto& s : str) {
13-
counts[s - 'a']++;
12+
for (char c : str) {
13+
counts[c - 'a']++;
1414
}
1515

1616
string key;
17-
for (const auto& c : counts) {
17+
for (int i : counts) {
1818
key.push_back('#');
19-
key.push_back(c + '0');
19+
key.push_back(i + '0');
2020
}
2121

2222
ht[key].push_back(str);

0051_n_queens/n_queens.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void dfs(int n, int row, int *stack, char ***solutions, int *count, int *
7373
/**
7474
* Return an array of arrays of size *returnSize.
7575
* The sizes of the arrays are returned as *returnColumnSizes array.
76-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
76+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
7777
*/
7878
char *** solveNQueens(int n, int* returnSize, int** returnColumnSizes)
7979
{

0056_merge_intervals/merge_intervals.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static int compare(const void *a, const void *b)
1111
/**
1212
* Return an array of arrays of size *returnSize.
1313
* The sizes of the arrays are returned as *returnColumnSizes array.
14-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
14+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
1515
*/
1616
int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes)
1717
{

0057_insert_interval/insert_interval.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ static int compare(const void *a, const void *b)
1010
/**
1111
* Return an array of arrays of size *returnSize.
1212
* The sizes of the arrays are returned as *returnColumnSizes array.
13-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
13+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
1414
*/
15-
int** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes)
15+
int** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval,
16+
int newIntervalSize, int* returnSize, int** returnColumnSizes)
1617
{
1718
int i, len = 0;
1819
int *tmp = malloc((intervalsSize + 1) * 2 * sizeof(int));

0059_spiral_matrix_ii/spiral_matrix.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Return an array of arrays of size *returnSize.
77
* The sizes of the arrays are returned as *returnColumnSizes array.
8-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
8+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
99
*/
1010
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes)
1111
{

0077_combinations/combinations.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdbool.h>
44
#include <string.h>
55

6+
67
static void dfs(int n, int k, int start, int *stack, int len,
78
int **results, int *count, int *col_sizes)
89
{
@@ -23,7 +24,7 @@ static void dfs(int n, int k, int start, int *stack, int len,
2324
/**
2425
* Return an array of arrays of size *returnSize.
2526
* The sizes of the arrays are returned as *returnColumnSizes array.
26-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
27+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
2728
*/
2829
int** combine(int n, int k, int* returnSize, int** returnColumnSizes) {
2930
int capacity = 10000;

0078_subsets/subsets.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,34 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5-
static void dfs(int *nums, int size, int start, int *buf,
5+
6+
static void dfs(int *nums, int size, int start, int *stack,
67
int len, int **sets, int *count, int *sizes)
78
{
89
int i;
910
sets[*count] = malloc(len * sizeof(int));
10-
memcpy(sets[*count], buf, len * sizeof(int));
11+
memcpy(sets[*count], stack, len * sizeof(int));
1112
sizes[*count] = len;
1213
(*count)++;
1314
for (i = start; i < size; i++) {
14-
buf[len] = nums[i];
15-
dfs(nums, size, i + 1, buf, len + 1, sets, count, sizes);
15+
stack[len] = nums[i];
16+
dfs(nums, size, i + 1, stack, len + 1, sets, count, sizes);
1617
}
1718
}
1819

1920
/**
2021
** Return an array of arrays of size *returnSize.
2122
** The sizes of the arrays are returned as *returnColumnSizes array.
22-
** Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
23+
** Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
2324
**/
2425
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes)
2526
{
2627
int capacity = 5000;
2728
int **sets = malloc(capacity * sizeof(int *));
28-
int *buf = malloc(numsSize * sizeof(int));
29+
int *stack = malloc(numsSize * sizeof(int));
2930
*returnColumnSizes = malloc(capacity * sizeof(int));
3031
*returnSize = 0;
31-
dfs(nums, numsSize, 0, buf, 0, sets, returnSize, *returnColumnSizes);
32+
dfs(nums, numsSize, 0, stack, 0, sets, returnSize, *returnColumnSizes);
3233
return sets;
3334
}
3435

0107_binary_tree_level_order_traversal_ii/bst_bfs.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5-
#define BST_MAX_LEVEL 800
65

76
struct TreeNode {
87
int val;
@@ -29,9 +28,9 @@ static void bfs(struct TreeNode *root, int **results, int *count, int *col_sizes
2928
/**
3029
** Return an array of arrays of size *returnSize.
3130
** The sizes of the arrays are returned as *returnColumnSizes array.
32-
** Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
31+
** Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
3332
**/
34-
static int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes)
33+
int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes)
3534
{
3635
if (root == NULL) {
3736
*returnSize = 0;
@@ -40,9 +39,9 @@ static int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** retu
4039

4140
int size = 1;
4241
*returnSize = 0;
43-
int **results = malloc(BST_MAX_LEVEL * sizeof(int *));
44-
*returnColumnSizes = malloc(BST_MAX_LEVEL * sizeof(int));
45-
memset(*returnColumnSizes, 0, BST_MAX_LEVEL * sizeof(int));
42+
int **results = malloc(800 * sizeof(int *));
43+
*returnColumnSizes = malloc(800 * sizeof(int));
44+
memset(*returnColumnSizes, 0, 800 * sizeof(int));
4645
bfs(root, results, returnSize, *returnColumnSizes, &size, 0);
4746

4847
int i, j;

0113_path_sum_ii/path_sum.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
56
struct TreeNode {
67
int val;
78
struct TreeNode *left;
@@ -25,7 +26,12 @@ static void dfs(struct TreeNode *node, int sum, int *stack, int len, int **resul
2526
}
2627
}
2728

28-
static int **pathSum(struct TreeNode *root, int sum, int **columnSizes, int *returnSize)
29+
/**
30+
* Return an array of arrays of size *returnSize.
31+
* The sizes of the arrays are returned as *returnColumnSizes array.
32+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
33+
*/
34+
int **pathSum(struct TreeNode *root, int sum, int *returnSize, int **returnColumnSizes)
2935
{
3036
if (root == NULL) {
3137
*returnSize = 0;
@@ -35,8 +41,8 @@ static int **pathSum(struct TreeNode *root, int sum, int **columnSizes, int *ret
3541
int level = 5000, cap = 1000;
3642
int *stack = malloc(level * sizeof(int));
3743
int **results = malloc(cap * sizeof(int *));
38-
*columnSizes = malloc(cap * sizeof(int));
39-
dfs(root, sum, stack, 0, results, *columnSizes, returnSize);
44+
*returnColumnSizes = malloc(cap * sizeof(int));
45+
dfs(root, sum, stack, 0, results, *returnColumnSizes, returnSize);
4046
return results;
4147
}
4248

@@ -76,13 +82,14 @@ int main(int argc, char **argv)
7682
n3[7].right = NULL;
7783

7884
int i, j, count = 0;
79-
int *sizes;
80-
int **list = pathSum(&root, 22, &sizes, &count);
85+
int *col_sizes, sum = 22;
86+
int **list = pathSum(&root, sum, &count, &col_sizes);
8187
for (i = 0; i < count; i++) {
82-
for (j = 0; j < sizes[i]; j++) {
88+
for (j = 0; j < col_sizes[i]; j++) {
8389
printf("%d ", list[i][j]);
8490
}
8591
printf("\n");
8692
}
93+
8794
return 0;
8895
}

0 commit comments

Comments
 (0)