• Leetcode: Generate Parentheses


    #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
    ", *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] = '';
      }
      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("
    ");
      }
      printf("===================
    ");
    }
    
    int main(int argc, char * argv[]) {
      int size = 0, n = 10;
      char ** result = generateParenthesis(n, &size);
      printf("generate success...
    ");
      print(result, n, size);
      printf("hello
    ");
      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] == '') break;
        }
        item[i] = '(';
        item[result->num*2] = '';
    
        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] == '') break;
        }
        item[i] = ')';
        item[result->num*2] = '';
    
        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] = '';
      struct Node * p = link_list_result.next;
      printf("1111111111
    ");
      generate(n, n, s, &link_list_result);
      printf("22222222222
    ");
      p=link_list_result.next;
      while(p != NULL) {
        printf("---> %s
    ", 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
    ", p->s);
        result[i++] = p->s;
        p = p->next;
      }
    
      printf("returnSize = %d", *returnSize);
    
      for(i=0; i<*returnSize; i++) {
        printf("------> %s
    ", 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("
    ");
      }
      printf("===================
    ");
    }
    
    int main(int argc, char * argv[]) {
      int size = 0, n = 3;;
      char ** result = generateParenthesis(n, &size);
      printf("generate success...
    ");
      print(result, n, size);
      printf("hello
    ");
      return 0;
    }

     

    两种方法的结果内容一样,但是次序会有差别,leetcode只认第二种。

  • 相关阅读:
    剑指Offer替换空格
    转载类的静态成员函数和静态成员变量的使用
    最长递增子序列长度LIS
    剑指Offer二维数组中的查找
    转载C语言:浮点数在内存中的表示
    7 CICD实战 持续集成71 kubernetes与cicd
    9 深入kubernetes服务调度与编排91 健康检查高可用的守护者
    项目成功的⑤大因素
    Ucos_II2.52源码中文译注
    SIP协议中的认证方式
  • 原文地址:https://www.cnblogs.com/tuhooo/p/9050106.html
Copyright © 2020-2023  润新知