Simple accepted JAVA solution


  • 0
    S
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            
            List<String> result = new ArrayList<String>();
            
            for(int n : nums)
            {
                if(n == Integer.MIN_VALUE)
                {
                    lower = n + 1;
                    continue;
                }
                
                int prev = n - 1;
                
                if(lower == prev) result.add(lower + "");
                else if(lower < prev) result.add(lower + "->" + prev);
                
                if(n == Integer.MAX_VALUE)
                {
                    return result;
                }
                lower = n + 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.