打印出所有可能的括号组合
以下为递归方法实现
[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;
};
补充:软件开发 , C++ ,