题解 | #括号生成#

括号生成

https://www.nowcoder.com/practice/c9addb265cdf4cdd92c092c655d164ca

#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return string字符串vector
     */
    vector<string> res;
    void dfs(int n , int count, string& s){
        if (s.size()==2*n) {
            if(count == 0){
                res.push_back(s);
                return;
            }

            return;
        }
        if(count+1<=n){
            s.push_back('(');
            count = count + 1;
            dfs(n, count, s);
            count = count - 1;
            s.pop_back();
        }
        if(count-1>=0) {
            s.push_back(')');
            count= count - 1;
            dfs(n,count,s);
            count = count +1;
            s.pop_back();
        }
    }
    vector<string> generateParenthesis(int n) {
        string s;
        dfs(n, 0, s);
        return res;
    }
};

全部评论

相关推荐

每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务