当前位置:网站首页>216. combined summation III enumeration method
216. combined summation III enumeration method
2022-06-24 08:52:00 【Mr Gao】
216. Combinatorial summation III
Find the sum of all the sums n Of k Combination of numbers , And the following conditions are met :
Use only numbers 1 To 9
Every number Use it once at most
return A list of all possible valid combinations . The list cannot contain the same combination twice , Combinations can be returned in any order .
Example 1:
Input : k = 3, n = 7
Output : [[1,2,4]]
explain :
1 + 2 + 4 = 7
There is no other matching combination .
Example 2:
Input : k = 3, n = 9
Output : [[1,2,6], [1,3,5], [2,3,4]]
explain :
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There is no other matching combination .
Example 3:
Input : k = 4, n = 1
Output : []
explain : There is no valid combination .
stay [1,9] Use... In scope 4 A different number , The minimum sum we can get is 1+2+3+4 = 10, because 10 > 1, No valid combination .
/** * 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 r[10]={
1,1,1,1,1,1,1,1,1,1};
int path[9];
int **result;
int resultTop=0;
void dfs(int k,int n,int size,int val,int index ){
int i;
if(size==k&& val==n){
int *temp = malloc(sizeof(int)*k);
for(int i =0;i<k;i++){
temp[i] = path[i];
}
result[resultTop++] = temp;
}
for(i=index+1;i<=9;i++){
if(r[i]==1){
r[i]=0;
path[size]=i;
dfs(k,n,size+1,val+i,i);
r[i]=1;
}
}
}
int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){
result = malloc(sizeof(int*) *90);
resultTop=0;
dfs(k,n,0,0,0);
*returnSize = resultTop;
*returnColumnSizes = malloc(sizeof(int*) * resultTop);
for(int i=0 ; i<resultTop ; i++){
(*returnColumnSizes)[i] = k;
}
return result;
}
边栏推荐
猜你喜欢
随机推荐
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
1704. 判断字符串的两半是否相似
Send custom events in QT
Floating error waiting for changelog lock
Summary of methods in numpy
SLAM14讲中Sophus包的安装问题
Xiaohei ai4code code baseline nibble 1
[force deduction 10 days SQL introduction] Day3
OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
Several schemes of PHP code encryption
常用表情符号
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
Win11 blank when using VIM to view content in cmder
2022 spring recruitment interview summary
1528. 重新排列字符串
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
Mysql数据(Liunx环境)定时备份
pymysql 向MySQL 插入数据无故报错
xtrabackup做数据备份
玄铁E906移植----番外0:玄铁C906仿真环境搭建








