C code version to solve this issue, O(n) time complex


  • 0

    /**

    • Return an array of size *returnSize.

    • Note: The returned array must be malloced, assume caller calls free().
      /
      char
      * findMissingRanges(int* nums, int numsSize, int lower, int upper, int* returnSize) {
      int j =0, i =0;
      int count =0;
      int start[numsSize];
      int end[numsSize];
      int cur = 0;
      int pre = lower-1;

      while (i < numsSize){
      cur = nums[i];
      if(cur-pre>1){
      count = count+1;
      start[j] = pre;
      end[j] = cur;
      j++;
      }
      pre = cur;
      i++;
      }
      if (upper+1 - pre > 1) {
      count++;
      start[j] = pre;
      end[j] = upper+1;
      j++;
      }

      char **array = (char *)malloc(sizeof(char)*count);
      *returnSize = count;
      for(int i =0; i < count; i++) {
      array[i]= (char *)malloc(sizeof(char)*25);
      if(start[i]+1 == end[i]-1) {
      sprintf(array[i], "%d", start[i]+1);
      } else {
      sprintf(array[i], "%d->%d", start[i]+1, end[i]-1);
      }
      }
      return array;
      }


Log in to reply
 

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