Skip to main content

Missing Ranges

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 132 ms)​

MetricValue
Runtime132 ms
Memory41.3 MB
Date2022-01-12
Solution
public class Solution {
public IList<string> FindMissingRanges(int[] nums, int lower, int upper) {
List<string> result = new List<string>();
if (nums.Length == 0 || nums == null)
{
result.Add(getRange(lower, upper));
return result;
}

if(nums[0] > lower)
result.Add(getRange(lower, nums[0]-1));
for (int i = 1; i < nums.Length; i++)
{
if(nums[i] != nums[i-1]+1)
result.Add(getRange(nums[i-1]+1, nums[i]-1));
}

if(nums[nums.Length-1]<upper)
{
result.Add(getRange(nums[nums.Length-1]+1, upper));
}

return result;
}

private string getRange(int lo, int hi)
{
return (lo == hi) ? $"{lo}" : $"{lo}->{hi}";
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed