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;
    }
};
相关推荐
苕皮蓝牙土豆2 分钟前
C++ map容器: 插入操作
开发语言·c++
顾子茵39 分钟前
c++从入门到精通(六)--特殊工具与技术-完结篇
android·开发语言·c++
byte轻骑兵42 分钟前
【Bluedroid】蓝牙HID DEVICE 报告发送与电源管理源码解析
c++·hid·bluedroid
孞㐑¥42 分钟前
Linux之基础IO
linux·开发语言·c++·经验分享·笔记
瓦力wow1 小时前
c语言 写一个五子棋
c语言·c++·算法
X-future4261 小时前
院校机试刷题第六天:1134矩阵翻转、1052学生成绩管理、1409对称矩阵
线性代数·算法·矩阵
丁一郎学编程1 小时前
优先级队列(堆)
java·数据结构
Codeking__1 小时前
前缀和——中心数组下标
数据结构·算法
爱喝热水的呀哈喽1 小时前
非线性1无修
算法
GG不是gg2 小时前
数据结构:二叉树一文详解
数据结构·青少年编程