zoukankan      html  css  js  c++  java
  • leetcode-Generate Parentheses

    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:

    "((()))", "(()())", "(())()", "()(())", "()()()"


    class Solution {
    public:
        vector<string> generateParenthesis(int n) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(n == 0) {
                return vector<string>(1, "");
            }
    
            if(n == 1) {
                return vector<string>(1, "()");
            }
    
            vector<string> left;
            vector<string> right;
            vector<string> svec;
            int i = 0, j = 0, k = 0;
            string str;
            for(k = n - 1; k >= 0; --k) {
                left = generateParenthesis(k);
                right = generateParenthesis(n - 1 - k);
                for(i = 0; i < left.size(); ++i) {
                    for(j = 0; j < right.size(); ++j) {
                        str.clear();
                        str.append("(");
                        str.append(left[i]);
                        str.append(")");
                        str.append(right[j]);
                        svec.push_back(str);
                    }
                }
            }
    
            return svec;
        }
    };
    这个递归有点麻烦 

    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    何为 ISAPI
    MacDown-The open source Markdown editor for OS X.
    Atom使用
    运维
    Perl
    Kaggle
    J2EE
    leetcode
    Tensorflow 学习笔记
    EXCEL公式及宏
  • 原文地址:https://www.cnblogs.com/vintion/p/4116856.html
Copyright © 2011-2022 走看看