leetcode-plusone

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

 

int* plusOne(int* digits, int digitsSize, int* returnSize)

{int* plusOne(int* digits, int digitsSize, int* returnSize)

{    if(digits == NULL){    return NULL; }

int n = digitsSize – 1;

while(n >= 0)

{ if(digits < 9)

{ digits[n]++;

*returnSize = digitsSize;

return digits;

} else

{ digits[n] = 0; n–; } }

int * newdigit = (int *)malloc((digitsSize + 1)*sizeof(int));

newdigit[0] = 1;

for(int i = 1; i < (digitsSzie + 1); i++)

{ newdigit[i] = digits[i-1];}

*returnSize = digitsSize + 1;

return newdigit;

}

posted @ 2018-04-21 20:13  HappyLee1103  阅读(144)  评论(0编辑  收藏  举报