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;
    }
};
相关推荐
程序大视界3 分钟前
【C++ 从基础到项目实战】C++(八):运算符重载——让你的类用起来像内置类型
开发语言·c++·cpp
z200509308 分钟前
今日算法(回溯全排列)
c++·算法·leetcode
不会C语言的男孩19 分钟前
C++ Primer 第6章:函数
开发语言·c++
码上有光24 分钟前
c++:多态
java·jvm·c++·多态·多态原理
Lumbrologist24 分钟前
【C++】零基础入门 · 第 18 节:互斥锁与线程同步
java·开发语言·c++
tangchao340勤奋的老年?25 分钟前
C++ OpenGL显示地图
c++·opengl
I Promise3432 分钟前
C++ 多线程编程:从入门到实战
开发语言·c++
邪修king40 分钟前
C++map_set封装 : 红黑树底层迭代器以及仿函数的运用
android·c语言·数据结构·c++·b树
牟师傅敲代码1 小时前
第2章:底层时间驱动机制
c++
并不喜欢吃鱼1 小时前
从零开始 C++------ 十四【C++ 数据结构】unordered_map/unordered_set 全解析:从使用到底层模拟实现
开发语言·数据结构·c++