当前位置:网站首页>46. 全排列-dfs双百代码
46. 全排列-dfs双百代码
2022-06-30 05:59:00 【Mr Gao】
46. 全排列-dfs双百代码
给定一个不含重复数字的数组 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]]
/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */
int f1(int n){
int re=1;
while(n>=1){
re=re*n;
n--;
}
return re;
}
int re_size;
void dfs(int *nums,int numsSize,int *r,int now_size,int *t,int **re){
int i;
if(now_size==numsSize){
re[re_size]=(int *)malloc(sizeof(int )*numsSize);
for(i=0;i<numsSize;i++){
re[re_size][i]=t[i];
}
re_size++;
}
else{
for(i=0;i<numsSize;i++){
if(r[i]==0){
t[now_size]=nums[i];
r[i]=1;
dfs(nums,numsSize,r,now_size+1,t,re);
r[i]=0;
}
}
}
}
int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
int size=f1(numsSize);
int **re=(int **)malloc(sizeof(int *)*size);
*returnColumnSizes=(int *)malloc(sizeof(int)*size);
int *t=(int *)malloc(sizeof(int )*numsSize);
int r[numsSize];
int i;
re_size=0;
for(i=0;i<size;i++){
(*returnColumnSizes)[i]=numsSize;
}
for(i=0;i<numsSize;i++){
r[i]=0;
}
dfs(nums,numsSize,r,0,t,re);
*returnSize=re_size;
return re;
}
边栏推荐
- Answer sheet for online assignment of "motor and drive" of Xijiao 21 autumn (IV) [standard answer]
- Leader: who can use redis expired monitoring to close orders and get out of here!
- 【LeetCode】236. Nearest common ancestor of binary tree
- [ansible series] fundamentals -01
- SparseArray
- Sword finger offer 29 Print matrix clockwise
- Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
- Solidy - fallback function - 2 trigger execution modes
- MySQL數據庫用戶管理
- Leetcode search insert location
猜你喜欢

About modifying dual system default startup item settings

接口中方法详解
![[GPU] basic operation of GPU (I)](/img/ce/0ca8c63525038fea64c40aabd17fc6.jpg)
[GPU] basic operation of GPU (I)

Implementation of property management system with ssm+ wechat applet

UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)

Sound net, debout dans le "sol" de l'IOT
![09- [istio] istio service entry](/img/48/86f8ec916201eefc6ca09c45a60a6a.jpg)
09- [istio] istio service entry

电脑查看WiFi使用密码

MySQL advanced SQL statement

Official win 10 image download
随机推荐
Leetcode search insert location
Leader: who can use redis expired monitoring to close orders and get out of here!
Gestion des utilisateurs de la base de données MySQL
Cisco VXLAN配置
MySQL storage system
CompletionService使用及原理(源码分析)
inno setup 最简单的自定义界面效果
Learning automation ppt
After getting these performance test decomposition operations, your test path will be more smooth
Prototype and prototype chain in JS
How to automatically renew a token after it expires?
luoguP2756 飞行员配对方案问题(最大流)
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
SparseArray
VLAN access mode
谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
STM32F103系列控制的OLED IIC 4针
C language code record
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
声网,站在物联网的“土壤”里