您的位置:首页 > 其它

Leetcode: Generate Parentheses

2013-10-17 06:57 330 查看
http://oj.leetcode.com/problems/generate-parentheses/

// Interesting questions
// It is very easy to generate duplicated result with normal dp
// The core idea is that from the start to the end of the string,
// we need to keep the property that cnt("(") is more than or equal to cnt(")")
class Solution {
public:
void Generate(int left, int right, string current, vector<string> &res){
if(left==0&&right==0) res.push_back(current);
if(left>0) Generate(left-1,right,current+"(",res);
if(right>left) Generate(left,right-1,current+")",res);
}
vector<string> generateParenthesis(int n) {
vector<string> res;
Generate(n,n,"",res);
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: