LeetCode46. Permutations

文章目录

一、题目

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example 1:

Input: nums = 1,2,3

Output: \[1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1]

Example 2:

Input: nums = 0,1

Output: \[0,1,1,0]

Example 3:

Input: nums = 1

Output: \[1]

Constraints:

1 <= nums.length <= 6

-10 <= numsi <= 10

All the integers of nums are unique.

二、题解

cpp 复制代码
class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    void backtracking(vector<int>& nums,vector<int>& used){
        if(path.size() == nums.size()){
            res.push_back(path);
            return;
        }
        for(int i = 0;i < nums.size();i++){
            if(used[i] == 1) continue;
            used[i] = 1;
            path.push_back(nums[i]);
            backtracking(nums,used);
            path.pop_back();
            used[i] = 0;
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        int n = nums.size();
        vector<int> used(n,0);
        backtracking(nums,used);
        return res;
    }
};
相关推荐
科学实验家6 分钟前
最小生成树:Prim,kruskal
数据结构·c++·算法
学习智者9 分钟前
《玄》IDE v3.6.3重磅发布:全功能修复与性能飞跃
开发语言·c++·ide·算法·中文语言 玄
Jasmine_llq11 分钟前
《P10263 [GESP202403 八级] 公倍数问题》
算法·数论·快速 io 优化·埃氏筛(倍数枚举)·线性遍历求和
2402_8828938613 分钟前
C++异常:从概念到实践的一篇博客
c++·异常
橘子汽水16813 分钟前
Leetcode 322 279 零钱兑换,完全平方数
算法·leetcode
kevin_kang19 分钟前
第01章 VoiceAgent 的总体架构与完整流程
算法
m0_7345717619 分钟前
深入理解C++ 析构函数<二>析构顺序
开发语言·c++
Lintongzg21 分钟前
KV-Cache 的显存账本:长上下文、并发与量化剪枝的取舍
算法·机器学习·剪枝
小孩玩什么29 分钟前
深入理解字符串匹配算法:BF算法,KMP算法
java·c语言·开发语言·数据结构·c++·算法
jsjzsl21 小时前
独立自由度框架下核聚变的本体论本质与商业化技术新路径
人工智能·python·算法