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 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
AI情绪识别开源7 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
罗西的思考8 小时前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
「QT(C++)开发工程师」8 小时前
C++ auto 用法详解
开发语言·c++
OPEN-F8 小时前
C++STL教程:容器适配器与实用工具
开发语言·c++
OPEN-F8 小时前
C++模板教程:变参模板、折叠表达式与SFINAE
java·开发语言·c++
hahaha60168 小时前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
有点。8 小时前
C++二叉搜索树进阶
开发语言·c++
HugoStudio_SWAN8 小时前
【擦除重绘】C++ 控制台动画:弹跳 Logo DVD 屏保效果
开发语言·c++·学习·程序人生
多弗朗皮卡丘9 小时前
算法详解4:买卖股票的最佳时机系列(上)
算法