码迷,mamicode.com
首页 > 其他好文 > 详细

Generate Parentheses

时间:2015-04-01 11:25:47      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:string   递归   

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

标签:string   递归   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!