首页 > 代码库 > Leetcode--Generate Parentheses
Leetcode--Generate Parentheses
Problem Description:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
class Solution { public: vector<string> res; void dfs(int n,int left,int right, string str) { if(left==right&&left==n) { res.push_back(str); return; } if(left<right||left>n) return; else { str+='('; dfs(n,left+1,right,str); str=str.substr(0,str.size()-1); str+=')'; dfs(n,left,right+1,str); str=str.substr(0,str.size()-1); } } vector<string> generateParenthesis(int n) { if(n<=0) return res; string s=""; dfs(n,0,0,s); return res; } };
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。