目录

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 <= nums[i] <= 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;
    }
};
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
末央&13 分钟前
【C++】list底层封装和实现
c++·windows·list
Zhichao_9714 分钟前
【UE5 C++课程系列笔记】35——HTTP基础——HTTP客户端异步请求API接口并解析响应的JSON
c++·ue5
aw34424 分钟前
tiny_dnn_test250101
人工智能·算法·dnn
风铃儿~33 分钟前
Redis过期key处理、内存淘汰策略与缓存一致性策略实践方案
java·数据结构·redis·微服务
Bryce-Lin37 分钟前
深度学习、图像算法学习记录
深度学习·学习·算法
大龄门外汉41 分钟前
数据结构之栈和队列
c语言·数据结构·学习·改行学it
AndrewHZ1 小时前
【图像处理基石】什么是自动曝光(AE)?
图像处理·人工智能·算法·计算机视觉·ae·isp算法·3a
阿猿收手吧!1 小时前
【QT】QPixmap QImage QBitmap QPicture
开发语言·c++·qt
攻城狮7号1 小时前
【第39节】windows编程:打造MFC版本任务管理器
c++·windows·mfc·任务管理器
朝九晚五ฺ1 小时前
【算法学习】链表篇:链表的常用技巧和操作总结
数据结构·学习·算法·链表