首页 > 其他分享 >39. 组合总和c

39. 组合总和c

时间:2024-03-08 17:45:13浏览次数:18  
标签:39 target 组合 returnSize int candidates array now 总和

/**
 * 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[100];
void dfs(int* candidates,int candidatesSize,int** array,int target,int* returnSize,int* column,int now,int n){
    if(now>target ) return;
    if(now==target){
        column[*returnSize]=n;
        for(int i=0;i<n;i++) array[*returnSize][i]=temp[i];
        (*returnSize)++;
        return;
    }
    for(int i=0;i<candidatesSize;i++){
        if(n!=0 && temp[n-1] >candidates[i]) continue;
        temp[n]=candidates[i];
        dfs(candidates,candidatesSize,array,target,returnSize,column,now+candidates[i],n+1);
    }
}

int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {
    *returnSize=0;
    int** array=(int**)malloc(sizeof(int*)*500);
    for(int i=0;i<500;i++) array[i]=(int*)malloc(sizeof(int)*50);
    int* column=(int*)malloc(sizeof(int)*500);
    dfs(candidates,candidatesSize,array,target,returnSize,column,0,0);
    printf("%d",*returnSize);
    *returnColumnSizes=column;
    return array;
}

多么痛的领悟啊。分配内存的时候分配少了。找了近2个小时错误。。

标签:39,target,组合,returnSize,int,candidates,array,now,总和
From: https://www.cnblogs.com/llllmz/p/18061409

相关文章

  • 17. 电话号码的字母组合c
    C语言字符串细节真多啊,搞了好久。/***Note:Thereturnedarraymustbemalloced,assumecallercallsfree().*/charc[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv"......
  • day58 动态规划part15 代码随想录算法训练营 392. 判断子序列
    题目:392.判断子序列我的感悟:理解难点:听课笔记:我的代码:通过截图:代码易错点:老师代码:扩展写法-双指针:classSolution:defisSubsequence(self,s:str,t:str)->bool:#初始化两个指针,分别指向s和t的第一个字符i,j=0,0#......
  • SqlServer:FOR XML PATH('')
    业务需求:需要将一个流程的所有节点办理人,接收时间,以每一条requestid为主,横向的排列起来展示。而OAe9里面,workflow_currentoperator表就是存节点接收人,接收时间的。 它的结构如下:一个requestid下面有很多节点数据,每个节点也可能重复,因为有办理人,抄送人。在结构上,我们需要将......
  • 力扣回溯之 39. 组合总和
    //剪枝优化classSolution{  publicList<List<Integer>>combinationSum(int[]candidates,inttarget){    List<List<Integer>>res=newArrayList<>();    List<Integer>path=newArrayList<>();    A......
  • 【力扣】组合总和3(组合的去重)
    问题描述注意,如果数组里有两个元素的值相同,那么这两个元素是可以出现在同一个组合里的:但是:如果按前面的思路分析的话,会发现结果中出现很多相同的组合。像这样:这很明显是由于两个相同的1造成的,因为当前的startindex对应第一个1时,向下一层递归后,starindex定位的还是1,。如......
  • 216. 组合总和 IIIc
    /***Returnanarrayofarraysofsize*returnSize.*Thesizesofthearraysarereturnedas*returnColumnSizesarray.*Note:Bothreturnedarrayand*columnSizesarraymustbemalloced,assumecallercallsfree().*/inttemp[10];voiddfs(int**......
  • 77. 组合C
    回溯其实就是抽象图的遍历过程这题数据实在太离谱了。/***Returnanarrayofarraysofsize*returnSize.*Thesizesofthearraysarereturnedas*returnColumnSizesarray.*Note:Bothreturnedarrayand*columnSizesarraymustbemalloced,assumecaller......
  • Vue学习笔记39--创建Vue脚手架
    创建Vue脚手架1.Vue脚手架是Vue官方提供的标准开发工具(开发平台)2.脚手架最新版本4.x3.文档:https://cli.vuejs.org/zh/操作步骤:第一步:(仅第一次执行):全局安装@vue/cli(commandlineinterface)注:安装钱建议先设置镜像==》npmconfigsetregisterhttps://registry.npm.taoba......
  • 【力扣】组合总数(回溯法)
    题目描述#include<iostream>#include<vector>usingnamespacestd;vector<vector<int>>res;vector<int>path;intaccumulate(vector<int>path){ intsum; for(inti=0;i<path.size();i++){ sum+=path[i]; } r......
  • CF38E Let's Go Rolling! 题解
    分析考虑DP。因为\(n\le3000\),我们可以直接枚举插针的位置。定义状态函数\(f_i\)表示在从左往右第\(i\)个小球的位置上插针的最小花费。枚举该小球右边第一个插针的位置,则\(i\)到\(j-1\)的小球都会滚到小球\(i\)的位置。代价为\(\sum\limits_{k=i}^{j-1}x_k-x_i......