合法括号对问题

合法括号对问题

题目描述:输入一个正整数n,输出合法的括号对,此题目为LeetCode原题22题。(笔试题)

Exp:

输入:4

输出:

        


#include<iostream>
#include<string>
#include<vector>
using namespace std;

void Generate(int left, int right, string s, vector<string>&res){
	if (left == 0 && right == 0){
		res.push_back(s);
	}
	if (left > 0){
		Generate(left - 1, right, s + '(', res);
	}
	if (right > 0 && left < right){
		Generate(left, right - 1, s + ')', res);
	}
}

int main(){
	int n;
	cin >> n;
	vector<string>res;
	Generate(n, n, "", res);
	for (vector<string> ::iterator t = res.begin(); t != res.end(); t++){
		cout << *t << endl;
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/heart_leader/article/details/79853638