Skip to content

Commit 45b23e5

Browse files
Rename
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent f755c7e commit 45b23e5

File tree

4 files changed

+52
-70
lines changed

4 files changed

+52
-70
lines changed

022_generate_paratheses/Makefile

Lines changed: 0 additions & 2 deletions
This file was deleted.

022_generate_paratheses/parentheses.c

Lines changed: 0 additions & 68 deletions
This file was deleted.

022_generate_parathesis/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test parenthesis.c

022_generate_parathesis/parenthesis.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
static void dfs(int n, int left, int right, char *stack, int len, char **results, int *count)
6+
{
7+
if (len == 2 * n) {
8+
results[*count] = malloc((len + 1) * sizeof(char));
9+
strcpy(results[(*count)++], stack);
10+
return;
11+
}
12+
13+
if (left < n) {
14+
stack[len] = '(';
15+
dfs(n, left + 1, right, stack, len + 1, results, count);
16+
}
17+
18+
if (right < left) {
19+
stack[len] = ')';
20+
dfs(n, left, right + 1, stack, len + 1, results, count);
21+
}
22+
}
23+
24+
/**
25+
** Return an array of size *returnSize.
26+
** Note: The returned array must be malloced, assume caller calls free().
27+
**/
28+
static char** generateParenthesis(int n, int* returnSize)
29+
{
30+
char *stack = calloc(2 * n + 1, sizeof(char));
31+
char **parentheses = malloc(5000 * sizeof(char *));
32+
*returnSize = 0;
33+
dfs(n, 0, 0, stack, 0, parentheses, returnSize);
34+
35+
return parentheses;
36+
}
37+
38+
int main(int argc, char **argv)
39+
{
40+
int i, count;
41+
if (argc != 2) {
42+
fprintf(stderr, "Usage: ./test 3\n");
43+
exit(-1);
44+
}
45+
char ** lists = generateParenthesis(atoi(argv[1]), &count);
46+
for (i = 0; i < count; i++) {
47+
printf("%s\n", lists[i]);
48+
}
49+
return 0;
50+
}

0 commit comments

Comments
 (0)