1. 程式人生 > >LeetCode 22 Generate Parentheses(生成括號)

LeetCode 22 Generate Parentheses(生成括號)

classSolution{public:/**
     * @param n n pairs
     * @return All combinations of well-formed parentheses
     */
    vector<string> generateParenthesis(int n){// Write your code here
        vector<string> res;string s;
        helper(s,res,0,0,n);return res;}void helper(string s,vector<string>
&res,int l,int r,int n){if(r==n){ res.push_back(s);}elseif(l==n){ s+=')'; helper(s,res,l,r+1,n);}else{if(l>r) helper(s+')',res,l,r+1,n); helper(s+'(',res,l+1,r,n);}}};