1
程式碼:
#include <iostream>
#include <vector>
using namespace std;
vector <string> all;
void gen(int n, int l, int r, string str) {
if (str.length() == n*2) {
all.push_back(str);
return;
}
if (l < n) gen(n, l+1, r, str+"(");
if (l > r) gen(n, l, r+1, str+")");
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n;
while (cin >> n) {
all.clear();
gen(n, 0, 0, "");
for (int i = 0, len = all.size() ; i < len ; i ++) cout << all[i] << "\n";
cout << "\n";
}
return 0;
}
明明測試結果為0.9秒,上傳上來卻TLE??請問為何??