TheAlgorithms-C/leetcode/src/66.c
dedsinQ 2215758b3b - Adding Roman to Integer (13.c)
- Adding Divide Two Integers (29.c)
- Adding Plus One (66.c)
- Adding Bitwise AND of Numbers Range (201.c)
- Readme updated
2019-10-26 16:06:02 +02:00

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;
}