missingRanges to store the missing ranges.nums is empty. If it is, add the range [lower, upper] to missingRanges and return it.lower is less than the first element of nums, add the range [lower, nums[0] - 1] to missingRanges.nums from the first to the second-to-last element:
[current element + 1, next element - 1] to missingRanges.upper is greater than the last element of nums, add the range [nums[-1] + 1, upper] to missingRanges.missingRanges.