首页 > 其他 > 详细

Generate Parentheses

时间:2015-04-01 11:24:54      阅读:269      评论:0      收藏:0      [点我收藏+]

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:

"((()))", "(()())", "(())()", "()(())", "()()()"

#include<iostream>
#include<vector>
#include<string>

using namespace std;
void creatpath(vector<string>&Result,string &path,int lnum,int rnum)
{
	if (lnum==0&&rnum==0){
		Result.push_back(path);
		return;
	}
	if (lnum>0)
		creatpath(Result,path+"(",lnum-1,rnum+1);
	if (rnum>0)
		creatpath(Result,path+")",lnum,rnum-1);
}

vector<string> generateParenthesis(int n) {
	vector<string>Result;
	string path;
	creatpath(Result,path,n,0);
	return Result;
}


 

Generate Parentheses

原文:http://blog.csdn.net/li_chihang/article/details/44803523

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!