C++ | Leetcode C++题解之第46题全排列

题目:

题解:

cpp 复制代码
class Solution {
public:
    void backtrack(vector<vector<int>>& res, vector<int>& output, int first, int len){
        // 所有数都填完了
        if (first == len) {
            res.emplace_back(output);
            return;
        }
        for (int i = first; i < len; ++i) {
            // 动态维护数组
            swap(output[i], output[first]);
            // 继续递归填下一个数
            backtrack(res, output, first + 1, len);
            // 撤销操作
            swap(output[i], output[first]);
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int> > res;
        backtrack(res, nums, 0, (int)nums.size());
        return res;
    }
};
相关推荐
kk哥88994 小时前
C++ 对象 核心介绍
java·jvm·c++
helloworddm4 小时前
WinUI3 主线程不要执行耗时操作的原因
c++
YoungHong19925 小时前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
无能者狂怒5 小时前
YOLO C++ Onnx Opencv项目配置指南
c++·opencv·yolo
im_AMBER5 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
集智飞行5 小时前
c++函数传参的几种推荐方式
开发语言·c++
LYFlied6 小时前
【每日算法】LeetCode 25. K 个一组翻转链表
算法·leetcode·链表
点云SLAM7 小时前
C++ Template(模板)解读和模板报错如何“逆向阅读”定位
c++·c++20·c++模版·c++高级应用·c++模版报错定位
明洞日记7 小时前
【数据结构手册008】STL容器完全参考指南
开发语言·数据结构·c++
农夫山泉2号8 小时前
【c++】——c++编译的so中函数有额外的字符
java·服务器·c++