diff options
Diffstat (limited to 'Computer_Science/leetcode/66-plus_one.c')
| -rw-r--r-- | Computer_Science/leetcode/66-plus_one.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Computer_Science/leetcode/66-plus_one.c b/Computer_Science/leetcode/66-plus_one.c new file mode 100644 index 0000000..63d151c --- /dev/null +++ b/Computer_Science/leetcode/66-plus_one.c @@ -0,0 +1,33 @@ +/** + * Return an array of size *returnSize. + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* plusOne(int* digits, int digitsSize, int* returnSize) { + int i; + int in = 1; + int *result; + + for(i = digitsSize - 1; i >=0; i--) { + if(digits[i] == 9 && in == 1) { + digits[i] = 0; + in = 1; + } else { + digits[i] += in; + in = 0; + } + } + + if(in == 1) { + result = malloc(sizeof(int) * (digitsSize + 1)); + result[0] = 1; + for(i = 1; i < digitsSize + 1; i++) { + result[i] = digits[i-1]; + } + *returnSize = digitsSize + 1; + } else { + result = digits; + *returnSize = digitsSize; + } + + return result; +} |
