四对括号可以有多少种匹配排列方式?

46.搜狐(运算):
四对括号可以有多少种匹配排列方式?比如两对括号可以有两种:()()和(())

#include
#include
#include 
using namespace std ;
void Print(vector v)
{
	for (vector::iterator beg=v.begin();beg!=v.end();++beg)
		cout<<*beg<<" ";
	cout< &v)
{
	int nLeftBrackets=0;
	int nRightBrackets=0;
	for (vector::iterator beg=v.begin();beg!=v.end();++beg)
	{
		if(*beg=='(')
			nLeftBrackets++;
		else
			nRightBrackets++;
		if(nRightBrackets>nLeftBrackets)
			return;
		if(nLeftBrackets+nRightBrackets==nSize&&nLeftBrackets==nRightBrackets)
			Print(v);
	}
	
	if (nLen>0)
	{
		v.push_back('(');
		MatchNums(nSize,nLen-1,v);
		v.pop_back();
		v.push_back(')');
		MatchNums(nSize,nLen-1,v);
		v.pop_back();
	}
}
int main()
{
	vector  v;
	int n=6;
	MatchNums(n,n,v);
	return 1;
}


你可能感兴趣的:(代码实现)