当前位置:网站首页>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;
}
边栏推荐
- 解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
- What is SRE? A detailed explanation of SRE operation and maintenance system
- 【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
- DataX User Guide
- 一文讲透,商业智能BI未来发展趋势如何
- K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
- Camera projection matrix calculation
- “论解不了数独所以选择做个数独游戏这件事”
- 2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
- 【牛客】HJ1 字符串最后一个单词的长度
猜你喜欢

MySQL | 存储《康师傅MySQL从入门到高级》笔记
![[team management] 25 tips for testing team performance management](/img/bd/0ef55630de43efcf5aa663f3099fce.jpg)
[team management] 25 tips for testing team performance management

2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。

China chip Unicorn Corporation

KaFormer个人笔记整理

Prompt code when MySQL inserts Chinese data due to character set problems: 1366
![[pytoch basic tutorial 31] youtubednn model analysis](/img/18/dbeab69894583f6e5230772ce44652.png)
[pytoch basic tutorial 31] youtubednn model analysis

What is graph neural network? Figure what is the use of neural networks?

【Pytorch基础教程31】YoutubeDNN模型解析

Mysql数据(Liunx环境)定时备份
随机推荐
GradScaler MaxClipGradScaler
Summary of methods in numpy
【LeetCode】387. 字符串中的第一个唯一字符
数组相向指针系列
4274. 后缀表达式
【Pytorch基础教程31】YoutubeDNN模型解析
Why can ping fail while traceroute can
DataX User Guide
What is the future development trend of Business Intelligence BI
1704. 判断字符串的两半是否相似
用VNC Viewer的方式远程连接无需显示屏的树莓派
玄铁E906移植----番外0:玄铁C906仿真环境搭建
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
liunx 更改 vsftpd 的端口号
rpiplay实现树莓派AirPlay投屏器
String to Base64
MySQL——SQL语句
所说的Get post:请求的区别,你真的知道了吗??????
The pie chart with dimension lines can set various parameter options
Using sonar for code checking