【leetcode】22括號生成

題目描述:

https://leetcode-cn.com/problems/generate-parentheses/

思路:

代碼實現:

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        helper(n, n, "", res);
        return res;
    }
    void helper(int left, int right, string out, vector<string>& res){
        if(left>right) return;
        if(left ==0 && right==0) res.push_back(out);
        else{
            if(left>0) helper(left-1, right, out+'(', res);
            if(right>0) helper(left, right-1, out+')', res);
        }
    }
};

參考:回溯與遞歸https://blog.csdn.net/ajianyingxiaoqinghan/article/details/79682147

參考:https://www.cnblogs.com/grandyang/p/4444160.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章