漏了 pop_back 剩几个解

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

这个版本撤销时只改回了 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 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论