My 0ms C solution


  • 0
    S
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    char** summaryRanges(int* nums, int numsSize, int* returnSize) {
      if (!numsSize)
        return NULL;
      int j = 0;
      int count = 1;
      int start[numsSize];
      int end[numsSize];
      start[0] = end[0] = nums[0];
      for (int i = 1; i < numsSize; i++)
        if (nums[i] != nums[i-1] + 1)
        {
          count++;
          end[j++] = nums[i-1];
          start[j] = end[j] = nums[i];
        }
      end[j] = nums[numsSize - 1];
      *returnSize = count;
      char** array = (char**) calloc (count, sizeof (char*));
      for (int i = 0; i < count; i++)
      {
        array[i] = (char*) calloc (25, sizeof (char));
        if (start[i] == end[i])
          sprintf (array[i], "%d", start[i]);
        else
          sprintf (array[i], "%d->%d", start[i], end[i]);
      }
      return array;
    }

Log in to reply
 

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