Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2be5ee9

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
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‎

Lines changed: 2 additions & 1 deletion
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-
staticint** 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‎

Lines changed: 10 additions & 9 deletions
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‎

Lines changed: 2 additions & 2 deletions
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-
staticint** 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‎

Lines changed: 4 additions & 4 deletions
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-
staticint** 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‎

Lines changed: 2 additions & 2 deletions
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-
staticint** 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‎

Lines changed: 4 additions & 4 deletions
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-
staticchar*** 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‎

Lines changed: 4 additions & 4 deletions
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 (constauto& s : str) {
13-
counts[s - 'a']++;
12+
for (char c : str) {
13+
counts[c - 'a']++;
1414
}
1515

1616
string key;
17-
for (constauto& 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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 3 additions & 2 deletions
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));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /