-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path77. Combinations.c
81 lines (70 loc) · 1.68 KB
/
77. Combinations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
*/
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
void bt(int *buff, int ***p, int *psz, int *pn, int n, int k, int d, int start) {
int i;
if (d == k) {
// all done
if (*psz == *pn) {
*psz *= 2;
*p = realloc(*p, *psz * sizeof(int *));
//assert(*p);
}
(*p)[*pn] = malloc(k * sizeof(int));
//assert((*p)[*pn]);
memcpy((*p)[(*pn) ++], buff, k * sizeof(int));
return;
}
for (i = start; i <= n; i ++) {
buff[d] = i;
bt(buff, p, psz, pn, n, k, d + 1, i + 1);
}
}
int** combine(int n, int k, int** columnSizes, int* returnSize) {
int **p, *buff;
int psz, pn;
pn = 0;
psz = 10;
p = malloc(psz * sizeof(int *));
buff = malloc(k * sizeof(int));
//assert(p && buff);
bt(buff, &p, &psz, &pn, n, k, 0, 1);
free(buff);
*columnSizes = malloc(pn * sizeof(int));
//assert(*columnSizes);
*returnSize = pn;
while (pn --) {
(*columnSizes)[pn] = k;
}
return p;
}
/*
Difficulty:Medium
Total Accepted:120.4K
Total Submissions:303.6K
Related Topics Backtracking
Similar Questions Combination Sum Permutations
*/