当前位置:网站首页>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;
}
};边栏推荐
- Recycling rental system 100% open source without encryption Mall + recycling + rental
- LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
- MySQL related knowledge
- 基于php动漫周边商城管理系统(php毕业设计)
- 数字图像处理 第十二章——目标识别
- ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
- RecycleView的使用
- Flink集群搭建
- The difference between groupByKey and reduceBykey
- HCIP---多生成树协议相关知识点
猜你喜欢

MySQL相关知识

如何防范 DAO 中的治理攻击?

SOM Network 2: Implementation of the Code

Analysis of the development trend of game metaverse

Shell programming conditional statement

ImportError: `save_weights` requires h5py. Problem solved

程序员必备的 “ 摸鱼神器 ” 来了 !

数字图像处理 第十二章——目标识别

ImportError: `save_weights` requires h5py.问题解决

Kubernetes第零篇:认识kubernetes
随机推荐
(*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
Based on php animation peripheral mall management system (php graduation design)
[Mobile Web] Mobile terminal adaptation
如何优雅的性能调优,分享一线大佬性能调优的心路历程
基于php动漫周边商城管理系统(php毕业设计)
感觉自己好傻
【C语言实现】整数排序-四种方法,你都会了吗、
回收租凭系统100%开源无加密 商城+回收+租赁
基于php酒店在线预定管理系统获取(php毕业设计)
统计单词数
Appendix A printf, varargs and stdarg a. 2 use varargs. H to realize the variable argument list
数据分析面试手册《指标篇》
shell specification and variables
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
Delicious this year
File operations of WEB penetration
AIDL communication
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
0DFS Medium LeetCode6134. Find the closest node to the given two nodes
2022 版 MySQL 巅峰教程,收藏好,慢慢看