Accepted Java solution after new testcases added


  • 0
    M

    I noticed that a few corner cases have been added and old solutions on leetcode cannot pass them.
    So I wrote this simple solution.

    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> result = new ArrayList<>();
            
            for (int num : nums) {
                if (num == Integer.MIN_VALUE) {
                    lower = num + 1;
                    continue;
                }
                int justBelow = num - 1;
                if (justBelow == lower) result.add(lower + "");
                else if (lower < justBelow) result.add(lower + "->" + justBelow);
                
                if (num == upper) return result;
                else lower = num + 1;
            }
            if (lower == upper) result.add(lower + "");
            else if (lower < upper) result.add(lower + "->" + upper);
            
            return result;
        }
    }
    

Log in to reply
 

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