zoukankan      html  css  js  c++  java
  • leetcode128-generate-parentheses

    题目描述

    给出n对括号,请编写一个函数来生成所有的由n对括号组成的合法组合。
    例如,给出n=3,解集为:
    "((()))", "(()())", "(())()", "()(())", "()()()"

    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:

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



    示例1

    输入

    复制
    1

    输出

    复制
    ["()"]
    
    示例2

    输入

    复制
    2

    输出

    复制
    ["(())","()()"]
    
    class Solution {
    public:
        /**
         *
         * @param n int整型
         * @return string字符串vector
         */
        void generateParenthesisAux(int n,int x ,int y,string s,vector<string> &ans){
            if (y==n)  ans.push_back(s);
            if (x<n) generateParenthesisAux(n, x+1, y,  s+"(", ans);
            if (x>y) generateParenthesisAux(n,  x, y+1, s+")", ans);
        }
        vector<string> generateParenthesis(int n) {
            // write code here
            vector <string> ans;
            generateParenthesisAux(n,0,0,"",ans);
            return ans;
        }
    };

  • 相关阅读:
    HTML_from
    HTML_img
    python_Django默认转换器
    python_虚拟环境
    python_正则表达式
    mysql_pymysql模块
    mysql_权限管理
    mysql_子查询
    sudo权限造成的故障
    22.Linux定时任务
  • 原文地址:https://www.cnblogs.com/hrnn/p/13413845.html
Copyright © 2011-2022 走看看