-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlusOne.c
34 lines (28 loc) · 1.02 KB
/
PlusOne.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>
/*You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* plusOne(int* digits, int digitsSize, int* returnSize){
int n = digitsSize;
for(int i = n - 1; i >= 0; i--)
{
if(digits[i] == 9)
{
digits[i] = 0;
}
else
{
digits[i] = digits[i] + 1;
*returnSize = n;
return digits;
}
}
// calloc() allocate the memory and set 0 to all of the integers
int* result = (int*)calloc((n + 1), sizeof(int));
*returnSize = n + 1;
result[0] = 1;
return result;
}