漏了 pop_back 剩几个解
这个版本撤销时只改回了 used,忘了把 path 弹出来。运行它:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void nopop_dfs(const vector<int>& a, vector<int>& path, vector<bool>& used, vector<vector<int>>& res) {
if (path.size() == a.size()) {
res.push_back(path);
return;
}
for (size_t i = 0; i < a.size(); i++) {
if (used[i]) continue;
used[i] = true;
path.push_back(a[i]);
nopop_dfs(a, path, used, res);
used[i] = false;
}
}
vector<vector<int>> perm_nopop(const vector<int>& a) {
vector<vector<int>> res;
vector<int> path;
vector<bool> used(a.size(), false);
nopop_dfs(a, path, used, res);
return res;
}
int main() {
cout << perm_nopop({1, 2, 3}).size() << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论