当前位置:网站首页>46.全排列
46.全排列
2022-08-01 21:48:00 【SY_XLR】
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:
输入:nums = [1]
输出:[[1]]
提示:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums 中的所有整数 互不相同
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/permutations
题意很好理解。
class Solution {
public:
vector<int> v;
vector<vector<int>> ans;
map<int,bool> visited;
void dfs(vector<int>& nums,int cnt,int size){
//cnt为v中元素个数
if(cnt == size){ //cnt等于nums的元素个数时结束递归
ans.push_back(v);
return;
}
for(int j = 0;j < size;j++){
if(visited[nums[j]] != 1){ //该处数值没有被访问
visited[nums[j]] = 1; //标记为已被访问
v.push_back(nums[j]);
dfs(nums,cnt+1,size);
visited[nums[j]] = 0; //重新标为未被访问
v.pop_back();
}
}
return;
}
vector<vector<int>> permute(vector<int>& nums) {
int size = nums.size();
dfs(nums,0,size);
return ans;
}
};边栏推荐
- Pytest: begin to use
- Today's sleep quality record 74 points
- 不卷了!入职字节跳动一周就果断跑了。
- shell programming conventions and variables
- Based on php online learning platform management system acquisition (php graduation design)
- Based on php online music website management system acquisition (php graduation design)
- scikit-learn no moudule named six
- Yizhou Financial Analysis | The intelligent transformation of bank ATM machines is accelerated; the new Internet loan regulations bring challenges
- 0DFS中等 LeetCode6134. 找到离给定两个节点最近的节点
- HCIP---多生成树协议相关知识点
猜你喜欢

Today's sleep quality record 74 points

KMP 字符串匹配问题

SAP ABAP OData 服务如何支持删除(Delete)操作试读版

shell specification and variables

迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation

游戏元宇宙发展趋势展望分析

HCIP---多生成树协议相关知识点

shell规范与变量

19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform

SQL injection of WEB penetration
随机推荐
可视化——Superset使用
毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
Delicious this year
基于php影视资讯网站管理系统获取(php毕业设计)
HCIP---Multiple Spanning Tree Protocol related knowledge points
MySQL related knowledge
Based on php Xiangxi tourism website management system acquisition (php graduation design)
数据分析面试手册《指标篇》
递归(各经典例题分析)
Spark集群搭建
scikit-learn no moudule named six
基于php旅游网站管理系统获取(php毕业设计)
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
小程序--独立分包&分包预下载
ImportError: `save_weights` requires h5py. Problem solved
Appendix A printf, varargs and stdarg a. 2 use varargs. H to realize the variable argument list
Based on php hotel online reservation management system acquisition (php graduation project)
Centos7--MySQL的安装
MySQL相关知识
Unity Shader general lighting model code finishing