My C code accepted with 2 ms


  • 3
    K
    int* plusOne(int* digits, int digitsSize, int* returnSize) {
        if (digits == NULL)
        {
            return NULL;
        }
        int n = digitsSize-1;
        while(n >= 0)
        {
            if(digits[n] < 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 < (digitsSize+1); i++)
        {
            newdigit[i] = digits[i-1];
        }
        *returnSize = digitsSize+1;
        return newdigit;
    }

  • 0
    W

    @keran.yang.5

    what the hell is that?

    newdigit[0] = 1;


  • 0
    D

    @keran.yang.5
    Note: The returned array must be malloced, assume caller calls free().

    How the caller know if the return array is new malloced or not?


Log in to reply
 

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