Very clean and easy to understand, Acc solution


  • 1
        public IList<string> FindMissingRanges(int[] nums, int lower, int upper)
        {
            IList<string> res = new List<string>();
            if (nums.Length == 0 || nums == null)
            {
                res.Add((lower == upper) ? upper.ToString() : lower + "->" + upper);
            }
    
            int find = lower;
    
            for (int i = 0; i < nums.Length; i++)
            {
                if (i > 0 && nums[i] == nums[i - 1]) continue;
                
                if(find!=nums[i])
                    res.Add((nums[i] - find == 1) ? (find).ToString() : find + "->" + (nums[i] - 1));
    
                find = nums[i]+1;
    
                if (i == nums.Length - 1 && nums[i] != upper)
                    res.Add((find == upper) ? upper.ToString() : find + "->" + upper);
            }
    
            return res;
        }
    

Log in to reply
 

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