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;
        }
    };
    这个递归有点麻烦 

    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    getopt 命令行参数解析
    Linux下使用indent整理代码
    终端常用快捷键
    gedit 乱码解决
    linux sysrq
    linux下的文件审计功能(audit inotify)
    gdb基本命令
    linux shell 字符截断
    linux 设置时间 date命令
    Ubuntu 时间同步
  • 原文地址:https://www.cnblogs.com/vintion/p/4116856.html
Copyright © 2011-2022 走看看