-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe66.c
29 lines (25 loc) · 792 Bytes
/
e66.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
// Beats 100% but this notably might have free() issues with output if it has a carry on the last digit :l
/**
* 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 + 1));
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 + 1] = (temp - 10);
} else {
carry = false;
output[i + 1] = temp;
}
}
*returnSize = (carry) ? digitsSize + 1 : digitsSize;
if (carry) {
output[0] = 1;
return output;
} else {
return output + 1;
}
}