Simple Solution in C


  • -1
    M

    /**

    • Return an array of size *returnSize.
    • Note: The returned array must be malloced, assume caller calls free().
      /
      int
      plusOne(int* digits, int digitsSize, int* returnSize) {
      for (int i = digitsSize - 1; i >= 0; --i) {
      if (digits[i] < 9) {
      digits[i]++;
      returnSize = digitsSize;
      int
      res = (int*)malloc(sizeof(int) * digitsSize);
      memcpy(res, digits, digitsSize * sizeof(int) / sizeof(unsigned char));
      return res;
      } else {
      digits[i] = 0;
      }
      }
      returnSize = digitsSize + 1;
      int
      res = (int*)malloc(sizeof(int) * (digitsSize + 1));
      res[0] = 1;
      memcpy(res + 1, digits, digitsSize * sizeof(int) / sizeof(unsigned char));
      return res;
      }

Log in to reply
 

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