-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe66 v2.c
33 lines (27 loc) · 906 Bytes
/
e66 v2.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
// Version that doesn't deal with the free() issue but uses some more memory in the process during shifting :v
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* plusOne(int* digits, int digitsSize, int* returnSize) {
int* output = (int*) malloc(sizeof(int) * digitsSize);
bool carry = true;
for (int i = digitsSize - 1; i >= 0; i--) {
int temp = digits[i] + ((carry) ? 1 : 0);
if (temp > 9) {
carry = true;
output[i] = (temp - 10);
} else {
carry = false;
output[i] = temp;
}
}
*returnSize = (carry) ? digitsSize + 1 : digitsSize;
if (!carry) {
return output;
}
int* output2 = (int*) malloc(sizeof(int) * (1 + digitsSize));
output2[0] = 1;
memcpy(output2 + 1, output, sizeof(int) * digitsSize);
free(output);
return output2;
}