LeetCode题目:Generate Parentheses

原题地址:https://leetcode.com/problems/generate-parentheses/

解决方法:回溯法

class Solution {
private:
    vector<string> coll;
    void helper(string s, int left, int right){
        if(left > right || left < 0 || right < 0)
            return;
        if(0 == left && 0 == right){
            coll.push_back(s);
            return;
        }
        string lString = s, rString = s;
        helper(lString += '(', left - 1, right);
        helper(rString += ')', left, right - 1);
    }
public:
    vector<string> generateParenthesis(int n) {
        string s;
        helper(s, n, n);
        return coll;
    }
};

 

posted @ 2016-03-08 10:34  Runnyu  阅读(151)  评论(0编辑  收藏  举报