-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffer-57-02-ContinuousSquenceWithSum.c
More file actions
71 lines (63 loc) · 1.39 KB
/
offer-57-02-ContinuousSquenceWithSum.c
File metadata and controls
71 lines (63 loc) · 1.39 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "minunit.h"
// https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/
/**
* 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 **findContinuousSequence(int target, int *returnSize, int **returnColumnSizes)
{
if (target <= 0)
{
*returnSize = 0;
*returnColumnSizes = 0;
return NULL;
}
int **output = (int **)malloc(sizeof(int *) * target);
*returnColumnSizes = (int *)malloc(sizeof(int) * target);
*returnSize = 0;
int left = 1, right = 1, sum = 0, len = target / 2;
while (left <= len)
{
if (sum < target)
{
sum += right;
right++;
}
else if (sum > target)
{
sum -= left;
left++;
}
else
{
int cols = right - left;
output[*returnSize] = malloc(sizeof(int) * cols);
(*returnColumnSizes)[*returnSize] = cols;
for (int i = left; i < right; i++)
{
output[*returnSize][i - left] = i;
}
(*returnSize)++;
sum -= left;
left++;
}
}
return output;
}
MU_TEST(test_case)
{
mu_check(5 == 7);
}
MU_TEST_SUITE(test_suite)
{
MU_RUN_TEST(test_case);
}
int main()
{
MU_RUN_SUITE(test_suite);
MU_REPORT();
return MU_EXIT_CODE;
}