-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0066_plus_one.c
41 lines (36 loc) · 930 Bytes
/
0066_plus_one.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
/**
* Author: Xiangyue Cai
* Date: 2019-10-8
*/
// Example 1:
// Input: [1,2,3]
// Output: [1,2,4]
// Explanation: The array represents the integer 123.
// Example 2:
// Input: [4,3,2,1]
// Output: [4,3,2,2]
// Explanation: The array represents the integer 4321.
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
// runtime: 0ms
// memory usage: 7.2mb
int*
plusOne(int* digits, int digitsSize, int* returnSize)
{
*returnSize = digitsSize + 1;
int *res = malloc(sizeof(int) * (*returnSize));
int carry = 0;
res[digitsSize] = (digits[digitsSize - 1] + 1 + carry) % 10;
carry = (digits[digitsSize - 1] + 1 + carry) / 10;
for (int i = digitsSize - 2 ; i > -1 ; --i) {
res[i + 1] = (digits[i] + carry) % 10;
carry = (digits[i] + carry) / 10;
}
if (carry) {
res[0] = 1;
return res;
}
--(*returnSize);
return res + 1;
}