leetcode 22 generate parentheses

OJ address

Leetcode website : 22. Generate Parentheses

Description

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

My Solution in C


#include <string.h>

int top = 0;

char** (int n, int* returnSize) {
if (n <= 0) {
*returnSize = 0;
return 0;
}
top = 0;
char **returnArray = calloc(3000, sizeof(char *));
char * newArray = calloc(n*2+1, sizeof(char));
dfs(0, n, returnArray, newArray, 0, 0);
*returnSize = top;
return returnArray;
}

void dfs(int index, int n, char **returnArray, char *newArray, int left, int right) {
if (right == n) {
newArray[n*2] = '