mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
22 lines
595 B
C
22 lines
595 B
C
|
/**
|
||
|
* Note: The returned array must be malloced, assume caller calls free().
|
||
|
*/
|
||
|
int* plusOne(int* digits, int digitsSize, int* returnSize){
|
||
|
for (int i = digitsSize-1; i >= 0; i--) {
|
||
|
if (digits[i] < 9) {
|
||
|
digits[i]++;
|
||
|
*returnSize = digitsSize;
|
||
|
return digits;
|
||
|
} else {
|
||
|
digits[i] = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int* newdigit = (int*)malloc((digitsSize+1) * sizeof(int));
|
||
|
newdigit[0] = 1;
|
||
|
for (int i = 1; i < (digitsSize+1); i++) {
|
||
|
newdigit[i] = digits[i-1];
|
||
|
}
|
||
|
*returnSize = digitsSize+1;
|
||
|
return newdigit;
|
||
|
}
|