首页 > 其他分享 >46. 全排列c

46. 全排列c

时间:2024-03-09 21:26:34浏览次数:21  
标签:numsSize count 排列 returnSize 46 int array

出息了!一次AC

/**
 * 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 temp[10];
int visit[10]={0};

void dfs(int* nums,int numsSize,int** array,int* returnSize,int* column,int count){
    if(count>numsSize) return;
    if(count==numsSize){
        column[*returnSize]=count;
        array[*returnSize]=(int*)malloc(sizeof(int)*numsSize);
        for(int i=0;i<count;i++) array[*returnSize][i]=temp[i];
        (*returnSize)++;
        return;
    }
    for(int i=0;i<numsSize;i++){
        if(visit[i]==0){
            temp[count]=nums[i];
            visit[i]=1;
            dfs(nums,numsSize,array,returnSize,column,count+1);
            visit[i]=0;
        }   
    }
}

int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
    *returnSize=0;
    int** array=(int**)malloc(sizeof(int*)*10000);
    int* column=(int*)malloc(sizeof(int)*10000);
    dfs(nums,numsSize,array,returnSize,column,0);
    *returnColumnSizes=column;
    return array;
}

结果:

标签:numsSize,count,排列,returnSize,46,int,array
From: https://www.cnblogs.com/llllmz/p/18063320

相关文章

  • 【力扣】排列问题(回溯法)
    问题描述排列问题的难点在于排列要求有序,并且在写的时候发现,如何在选择后面的元素后回过头去选择前面的元素,这是很难处理的,在前面的组合问题中,我们都是用startindex来处理,而在这里就行不通了。容易想到的一种解决方法就是另外设置一个与nums长度相同的used数组来记录元素的遍历......
  • CF1846D Rudolph and Christmas Tree 题解
    因为\(n\)个三角形有重叠部分,所以我们可以倒序处理每个三角形,并对其进行分类讨论:若当前三角形编号为\(n\),则直接将总面积加上\(\dfrac{d\timesh}{2}\)。否则,再次分出两种情况:若当前三角形的\(y_i+h>y_{i+1}\)(即编号为\(i,i+1\)的三角形有重叠),则如下图所示:......
  • P2746 [USACO5.3] 校园网Network of Schools
    原题链接题解把奶牛看成点,赠送列表关系看成有向边,这样这道题就成了对强连通分量缩点,然后找出这个新图中入度为零的点有几个,出度为零的点有几个code#include<bits/stdc++.h>usingnamespacestd;vector<int>G[105];intlen=0,cnt=0;intbelong[105]={0};intin[105]={0},......
  • LeetCodeHot100 1.两数之和 46.字母异位词分组 128.最长连续序列
    1.两数之和https://leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-100-likedpublicint[]twoSum(int[]nums,inttarget){HashMap<Integer,Integer>map=newHashMap<>();for(inti=0;i<nums.l......
  • 代码随想录算法训练营第三十八天| ● 理论基础 ● 509. 斐波那契数 ● 70. 爬楼梯
    理论基础 代码随想录(programmercarl.com)动态规划的五部曲:确定dp数组(dptable)以及下标的含义确定递推公式dp数组如何初始化确定遍历顺序举例推导dp数组斐波那契数 题目链接:509.斐波那契数-力扣(LeetCode)思路:还好。classSolution{public:intfib(intn)......
  • 代码随想录算法训练营第三十八天 | 746. 使用最小花费爬楼梯,、70. 爬楼梯,509. 斐波那
     509.斐波那契数 已解答简单 相关标签相关企业 斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:F(0)=0,F(1)=1F(n)=F(n-1)+F(n-2),其中n>......
  • 「杂题乱刷」CF1846E1 & CF1846E2
    E1链接一眼题。直接预处理即可。时间复杂度\(O(n\log_2(n))\)。难度1。代码:点击查看代码/*Tips:你数组开小了吗?你MLE了吗?你觉得是贪心,是不是该想想dp?一个小时没调出来,是不是该考虑换题?*/#include<bits/stdc++.h>usingnamespacestd;#definemapunordered_m......
  • p4632-solution
    P4632Solutionlink对时间扫描线,就变成支持单点加入删除一个颜色点,求所有颜色距离某个点的距离最大值。考虑二分答案,现在就是要检验\([x-mid,x+mid]\)内是否有\(1\simk\)颜色的点各至少一个。数颜色可以考虑维护\(pre_i\)表示上一个与该点同色的位置。然后区间\([l,r]......
  • uniapp开发微信小程序,动态排列组件的解决方案。
    微信小程序开发里面,并不支持<component:is="item",虽然微信小程序提供了WXML提供模板(template),对于uniapp并不管用,编译后,所以解决方案,只有目前(截止2022-04-15)只有两个:1.使用v-if,遍历组件,判断位置,来显示组件,达到排列要求2.第二种没那么麻烦,比较神奇,使用flex布局的order属性,外层......
  • 246. 区间最大公约数
    #include<cstdio>#include<cstring>#include<iostream>#include<algorithm>usingnamespacestd;typedeflonglongLL;constintN=500010;intn,m;LLw[N];structNode{intl,r;LLsum,d;}tr[N*4];LLgcd(LL......