zoukankan      html  css  js  c++  java
  • 括号生成(回溯法)

    /*
    有效的括号--回溯法。
    */
    #include<stdio.h>
    #include<malloc.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #include<string.h>
    #include <iostream>
    void generate(int left,int right,int n,char *str,int index,char** rs,int *returnSize){
        if(left==n&&right==n){
            rs[(*returnSize)]=(char*)malloc(sizeof(char)*(2*n+1));
            str[index]='';
            strcpy(rs[(*returnSize)],str);
            (*returnSize)++;
            printf("%s ",str);
            return;
        }
        if(left<n){
            str[index]='(';
            generate(left+1,right,n,str,index+1,rs,returnSize);
        }
        if(right<n&&right<left){
            str[index]=')';
            generate(left,right+1,n,str,index+1,rs,returnSize);
        }
    }
    char ** generateParenthesis(int n, int* returnSize){
        char *str=(char*)malloc(sizeof(char)*(2*n+1));
        char **rs=(char**)malloc(sizeof(char*)*2500);
        (*returnSize)=0;
        generate(0,0,n,str,0,rs,returnSize);
    }
    
    int main()
    {
        char **rs=(char**)malloc(sizeof(char*)*2500);
        int *returnSize=(int*)malloc(sizeof(int)*(7));
        rs=generateParenthesis(3,returnSize);
        return 0;
    }
  • 相关阅读:
    闭包函数+装饰器(第十一天)
    函数初接触2
    函数初接触
    第八天
    第八天
    第七天
    day4—day6作业(补发)
    第六天
    第五天
    python基础学习-常用模块的使用(扩展补充,高级使用)(三)
  • 原文地址:https://www.cnblogs.com/zhaohuan1996/p/12626715.html
Copyright © 2011-2022 走看看