-
Notifications
You must be signed in to change notification settings - Fork 119
/
66.c
34 lines (32 loc) · 784 Bytes
/
66.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
#include <stdio.h>
#include <stdlib.h>
int* plusOne(int* digits, int digitsSize, int* returnSize) {
int *ans = (int *)malloc((digitsSize + 1) * sizeof(int));
*returnSize = digitsSize;
int i;
int sum = 1; /* plus one */
for (i = digitsSize - 1; i >= 0; i--) {
sum += digits[i];
ans[i + 1] = sum % 10;
sum /= 10;
}
if (sum) {
ans[0] = sum;
*returnSize = *returnSize + 1;
return ans;
}
else {
return ans + 1;
}
}
int main() {
int A[] = { 9, 9, 9 };
int retSize = 0;
int *ans = plusOne(A, sizeof(A) / sizeof(A[0]), &retSize);
int i;
for (i = 0; i < retSize; i++) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}