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

39. 组合总和c

时间:2024-03-21 19:15:29浏览次数:30  
标签:39 target 组合 returnSize int sum candidatesSize array 总和

/**
 * 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[50];

void dfs(int* candidates,int candidatesSize,int target,int* returnSize,int* column,int** array,int index,int count,int sum){
    if(index>candidatesSize || sum>target  ) return;
    if(index==candidatesSize && sum !=target) return;
    if(sum==target){
        printf("%d",sum);
        array[*returnSize]=(int*)malloc(sizeof(int)*count);
        column[*returnSize]=count;
        for(int i=0;i<count;i++) array[*returnSize][i]=temp[i];
        (*returnSize)++;
        return;
    }
    int use[50]={0};
    for(int i=index;i<candidatesSize;i++){
        if(use[candidates[i]]==0){
            use[candidates[i]]=1;
            temp[count]=candidates[i];
            dfs(candidates,candidatesSize,target,returnSize,column,array,i,count+1,sum+candidates[i]);
        }
    }
}

int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {
    *returnSize=0;
    int* column=(int*)malloc(sizeof(int)*50000);
    int** array=(int**)malloc(sizeof(int*)*50000);
    dfs(candidates,candidatesSize,target,returnSize,column,array,0,0,0);
    *returnColumnSizes=column;
    return array;
}

 

标签:39,target,组合,returnSize,int,sum,candidatesSize,array,总和
From: https://www.cnblogs.com/llllmz/p/18088057

相关文章

  • 40. 组合总和 IIc
    /***Returnanarrayofarraysofsize*returnSize.*Thesizesofthearraysarereturnedas*returnColumnSizesarray.*Note:Bothreturnedarrayand*columnSizesarraymustbemalloced,assumecallercallsfree().*/inttemp[150];intcmp(const......
  • 17. 电话号码的字母组合c
    /***Note:Thereturnedarraymustbemalloced,assumecallercallsfree().*/chartemp[10];charc[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv",&q......
  • 77. 组合c
    /***Returnanarrayofarraysofsize*returnSize.*Thesizesofthearraysarereturnedas*returnColumnSizesarray.*Note:Bothreturnedarrayand*columnSizesarraymustbemalloced,assumecallercallsfree().*/inttemp[30];voiddfs(int**......
  • [转帖]Evaluating Garnet's Performance Benefits
    EvaluatingGarnet'sPerformanceBenefitsEvaluatingGarnet'sPerformanceBenefits|Garnet(microsoft.github.io) Wehavetested Garnet thoroughlyinavarietyofdeploymentmodes:SamelocalmachineforclientandserverTwolocalmachines-......
  • CF938E-组合数
    link:https://codeforces.com/contest/938/problem/E题意:给一个序列\(a\),按如下方式计算\(f_a\):初始\(f_a=0,M=1\)对每个\(2\leqi\leqn\),如果\(a_M<a_i\),\(f_a\tof_a+a_M\),然后\(M=i\)对所有\(a\)的排列计算\(f_a\)并其在模\(10^9+7\)下的和。\(1\leqn\leq......
  • ACCESS 关于使用VBA选择路径时提示"方法'FileDislog作用于对象'_Application’时失败"
    以下是源码:PrivateSubCommand0_Click()'打开文件选择对话框WithApplication.FileDialog(msoFileDialogFilePicker).AllowMultiSelect=False.Filters.Clear.Filters.Add"Excel文件","*.xls;*.xlsx",1I......
  • Copula估计边缘分布模拟收益率计算投资组合风险价值VaR与期望损失ES|附代码数据
    全文链接:http://tecdat.cn/?p=24753最近我们被客户要求撰写关于风险价值的研究报告,包括一些图形和统计输出。在这项工作中,我通过创建一个包含四只基金的模型来探索copula,这些基金跟踪股票、债券、美元和商品的市场指数摘要然后,我使用该模型生成模拟值,并使用实际收益和模拟收......
  • Linux环境运行python项目提示No module named '_ssl'
    版本python3.11.4控制台错误提醒File"/usr/local/python3/lib/python3.11/ssl.py",line100,in<module> import_ssl#ifwecan'timportit,lettheerrorpropagate ^^^^^^^^^^^ModuleNotFoundError:Nomodulenamed'_ssl'错误原因:ce......
  • 代码随想录算法训练营第十三天|239. 滑动窗口最大值、347.前 K 个高频元素、总结
    题目:239.滑动窗口最大值文章链接:代码随想录视频链接:LeetCode:239.滑动窗口最大值题目链接:力扣题目链接图释:classSolution{public://自己定义一个优先队列classMyQueue{public: deque<int>deq; //弹出 voidpop(intvalue){ //当输入的数组与队顶......
  • 代码随想录 第二十四天| ●回溯 理论基础 ● 77. 组合
    回溯理论基础:回溯三部曲:制定回溯函数的参数和返回值确定回溯终止条件确定回溯遍历过程 回溯模板voidbacktracking(参数){if(终止条件){存放结果;return;}for(选择:本层集合中元素(树中节点孩......