#include <stdio.h> #include <stdlib.h> /** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ char** generateParenthesis(int n, int* returnSize) { char ** result = NULL; if(n == 1) { /* if n == 1 */ result = (char **) malloc(sizeof(char *) * 1); result[0] = (char *) malloc(sizeof(char) * 2 * n); result[0][0] = '('; result[0][1] = ')'; *returnSize = 1; } else { /* if n > 1 */ int subSize = 0; char ** subResult = generateParenthesis(n-1, &subSize); *returnSize = subSize * 3 - 1; printf("returnSize = %d\n", *returnSize); result = (char **) malloc(sizeof(char *) * (*returnSize)); int i=0,j=0; for(i=0; i<subSize; i++) { result[i] = (char *) malloc(sizeof(char) * 2 * n); result[i][0] = '('; result[i][2*n-1] = ')'; for(j=0; j<2*(n-1); j++) { result[i][j+1] = subResult[i][j]; } } int m = subSize; for(i=0; i<subSize-1; i++) { result[i+m] = (char *) malloc(sizeof(char) * 2 * n); result[i+m][2*n-2] = '('; result[i+m][2*n-1] = ')'; for(j=0; j<2*(n-1); j++) { result[i+m][j] = subResult[i][j]; } } m = subSize + subSize - 1; for(i=0; i<subSize-1; i++) { result[i+m] = (char *) malloc(sizeof(char) * 2 *n); result[i+m][0] = '('; result[i+m][1] = ')'; for(j=0; j<2*(n-1); j++) { result[i+m][j+2] = subResult[i][j]; } } result[*returnSize-1] = (char *) malloc(sizeof(char) * 2 * n); result[*returnSize-1][0] = '('; result[*returnSize-1][1] = ')'; for(j=0; j<2*(n-1); j++) { result[*returnSize-1][j+2] = subResult[i][j]; } for(i=0; i<subSize; i++) { free(subResult[i]); } free(subResult); } char ** append = (char **) malloc(sizeof(char *) * (*returnSize)); int k=0,j=0; for(k=0; k<(*returnSize); k++) { append[k] = (char *) malloc(sizeof(char) * (2 * n+1)); for(j=0; j< 2*n; j++) { append[k][j] = result[k][j]; } append[k][j+1] = '\0'; } return append; } void print(char ** result, int n, int size) { int i=0, j=0; for(i=0; i<size; i++) { for(j=0; j<2*n; j++) { printf("%c", result[i][j]); } printf("\n"); } printf("===================\n"); } int main(int argc, char * argv[]) { int size = 0, n = 10; char ** result = generateParenthesis(n, &size); printf("generate success...\n"); print(result, n, size); printf("hello\n"); return 0; }
改进版,欣赏一下鄙人的黑暗代码:
#include <stdio.h> #include <stdlib.h> struct Node { char * s; struct Node * next; int num; }; void generate(int leftNum, int rightNum, char * s, struct Node * result) { if(leftNum == 0 && rightNum == 0) { struct Node * p = result; while(p->next != NULL) p = p->next; p->next = (struct Node *) malloc(sizeof(struct Node)); p = p->next; p->next = NULL; p->s = s; } if(leftNum > 0) { char * item = (char *) malloc(sizeof(char) * (result->num * 2 + 1)); int i=0; for(i=0; i<result->num*2 + 1; i++) { item[i] = s[i]; } for(i=0; i<result->num*2 + 1; i++) { if(s[i] == '\0') break; } item[i] = '('; item[result->num*2] = '\0'; generate(leftNum-1, rightNum, item, result); } if(rightNum > 0 && leftNum < rightNum) { char * item = (char *) malloc(sizeof(char) * (result->num * 2 + 1)); int i=0; for(i=0; i<result->num*2 + 1; i++) { item[i] = s[i]; } for(i=0; i<result->num*2 + 1; i++) { if(s[i] == '\0') break; } item[i] = ')'; item[result->num*2] = '\0'; generate(leftNum, rightNum-1, item, result); } } /** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ char** generateParenthesis(int n, int* returnSize) { struct Node link_list_result; link_list_result.num = n; link_list_result.next = NULL; char * s = (char *) malloc(sizeof(char) * (2*n + 1)); int i=0; for(i=0; i<2*n+1; i++) s[i] = '\0'; struct Node * p = link_list_result.next; printf("1111111111\n"); generate(n, n, s, &link_list_result); printf("22222222222\n"); p=link_list_result.next; while(p != NULL) { printf("---> %s\n", p->s); p = p->next; } int total = 0; p=link_list_result.next; while(p != NULL) { total++; p = p->next; } *returnSize = total; char ** result = (char **) malloc(sizeof(char *) * total); p = link_list_result.next; i=0; while(p != NULL) { printf("set...%s\n", p->s); result[i++] = p->s; p = p->next; } printf("returnSize = %d", *returnSize); for(i=0; i<*returnSize; i++) { printf("------> %s\n", result[i]); } return result; } void print(char ** result, int n, int size) { int i=0, j=0; for(i=0; i<size; i++) { for(j=0; j<2*n; j++) { printf("%c", result[i][j]); } printf("\n"); } printf("===================\n"); } int main(int argc, char * argv[]) { int size = 0, n = 3;; char ** result = generateParenthesis(n, &size); printf("generate success...\n"); print(result, n, size); printf("hello\n"); return 0; }
两种方法的结果内容一样,但是次序会有差别,leetcode只认第二种。