当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

The form image uploaded in chorme cannot view the binary image information of the request body

Become an IEEE student member

为什么ping不通,而traceroute却可以通
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

input的聚焦后的边框问题

One article explains in detail | those things about growth

Database migration from PostgreSQL to MySQL

表单图片上传在Chorme中无法查看请求体的二进制图片信息

【牛客】HJ1 字符串最后一个单词的长度

【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
随机推荐
Xiaohei ai4code code baseline nibble 1
解决:模型训练时loss出现nan
一文详解|增长那些事儿
xtrabackup做数据备份
MySQL | 视图《康师傅MySQL从入门到高级》笔记
String to Base64
等保备案是什么意思?应该去哪里办理备案?
打印出来的对象是[object object],解决方法
【LeetCode】415. 字符串相加
leetcode 1268. Search suggestions system
数据库迁移从PostgreSQL迁移到 MYSQL
K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
Determination of monocular and binocular 3D coordinates
[pytoch basic tutorial 31] youtubednn model analysis
数据中台:数据中台技术架构详解
liunx服务器 telnet 带用户名 端口登陆方法
Rsync for file backup
【Pytorch基础教程31】YoutubeDNN模型解析
input的聚焦后的边框问题
剑指 Offer 55 - I. 二叉树的深度-dfs法