2024-03-19 02:45:09 +08:00

51 lines
981 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
/**
* https://leetcode-cn.com/problems/plus-one/description/
* +1(s)
* 不断优化达到了0ms。。。。虽然是道简单题
*/
int *plusOne(int *digits, int digitsSize, int *returnSize)
{
int *ret = malloc(sizeof(int) * (digitsSize + 1));
*returnSize = digitsSize;
int next = 1, pos;
for (int i = digitsSize; i > 0; i--)
{
ret[i] = digits[i - 1] + next;
if (ret[i] == 10)
{
ret[i] = 0;
next = 1;
}
else
{
next = 0;
}
}
if (next)
{
ret[0] = 1;
(*returnSize)++;
}
else
{
ret = &ret[1];
}
return ret;
}
int main()
{
int a[] = {9, 8}, ret = 0;
int *retArray = plusOne(a, 2, &ret);
for (int i = 0; i < ret; i++)
{
printf("%d\t", retArray[i]);
}
printf("%d\n", ret);
getchar();
return 0;
}