C solution with comments.


  • 0
    C
    int* plusOne(int* digits, int digitsSize, int* returnSize) {
    int carry = 1, i = digitsSize;
    int* ret = (int*)malloc(sizeof(int)*(digitsSize+1));
    while (i > 0) {
        ret[i] = (digits[i-1]+carry)%10;
        carry = (digits[i-1]+carry)/10;
        i--;
    }
    if (!ret[1]) { 
        // size increased 1, set the first digit
        ret[0] = 1;
        *returnSize = digitsSize+1;
        return ret;
    } else {
        *returnSize = digitsSize;
        return ret+1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.