C code runs in 3ms, slowly but clearly? Or someone may give me several suggestions...


  • 0
    L
    int* plusOne(int* digits, int digitsSize, int* returnSize) {
        int n = digitsSize - 1;
        if (digits[n] < 9) {
            *returnSize = digitsSize;
            digits[n]++;
            return digits;
        } else {
            int countNine = 0;
            for (; n >= 0; n--) {
                if (digits[n] == 9) {
                    countNine++;
                    digits[n] = 0;
                } else {
                    digits[n]++;
                    break;
                }
            }
            if (countNine != digitsSize) {
                *returnSize = digitsSize;
                return digits;
            } else {
                int newSize = digitsSize + 1;
                *returnSize = newSize;
                int* newDigits = (int*)malloc(sizeof(int) * newSize);
                newDigits[0] = 1;
                for (int i = 1; i < newSize; i++) {
                    newDigits[i] = digits[i - 1];
                }
                return newDigits;
            }
        }
    }
    

Log in to reply
 

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