以下為遞歸方法實現
[cpp]
#include <vector>
#include <iostream>
#define N 3
void print(int left, int right, std::vector<char> v)
{
if (left == right && left == N)
{
for (size_t i = 0; i < v.size(); i++)
{
std::cout << v[i];
}
std::cout << std::endl;
return;
}
if (left > right)
{
v.push_back(')');
print(left, right + 1, v);
if (left < N)
{
v.pop_back();
v.push_back('(');
print(left + 1, right, v);
}
}
else
{
v.push_back('(');
print(left + 1, right, v);
}
};
int main()
{
std::vector<char> v;
print(0, 0, v);
return 0;
};