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;
    }
};
相关推荐
易只轻松熊1 小时前
C++(21):fstream的读取和写入
开发语言·c++
虾球xz2 小时前
游戏引擎学习第292天:实现蛇
c++·学习·游戏引擎
jjkkzzzz8 小时前
Linux下的c/c++开发之操作Redis数据库
数据库·c++·redis
pystraf8 小时前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
Funny-Boy9 小时前
菱形继承原理
c++
飞川撸码9 小时前
【LeetCode 热题100】739:每日温度(详细解析)(Go语言版)
算法·leetcode·golang
Nobkins10 小时前
2021ICPC四川省赛个人补题ABDHKLM
开发语言·数据结构·c++·算法·图论
海棠蚀omo11 小时前
C++笔记-红黑树
开发语言·c++·笔记
一个Potato11 小时前
C++笔试题(金山科技新未来训练营):
c++·科技
休息一下接着来11 小时前
C++ I/O多路复用
linux·开发语言·c++