Finding all combinations of well-formed brackets

后端 未结 29 1644
盖世英雄少女心
盖世英雄少女心 2020-11-28 02:34

This came up while talking to a friend and I thought I\'d ask here since it\'s an interesting problem and would like to see other people\'s solutions.

The task is to

29条回答
  •  没有蜡笔的小新
    2020-11-28 03:13

    An attempt with memoization:

    void push_strings(int i, int j ,vector> &T){
        for (int k=0; k< T[j].size(); ++k){
            for (int l=0; l< T[i - 1 - j].size(); ++l){
                string s = "(" + T[j][k] + ")" + T[i-1 - j][l];
                T[i].push_back(s);
            }
        }
    }
    
    vector generateParenthesis(int n) {
        vector> T(n+10);
        T[0] = {""};
    
        for (int i =1; i <=n; ++i){
            for(int j=0; j

提交回复
热议问题