Passes all the test and easy to understand


  • 1
    P
    public class Solution {
       public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> res = new ArrayList<String>();
            int range = lower;
            for ( int i = 0; i < nums.length; i++) {
                if ( nums[i] > range) {
                    res.add(getValidRange(range, nums[i] - 1));
                }
                if ( upper == nums[i]) { // handles overflow
                    return res;
                }
                range = nums[i] + 1;  
              
                
            }
            if ( range <= upper) {
                res.add(getValidRange(range, upper));
            }
    
            return res;
        }
        private String getValidRange(int lower, int upper) {
            return upper > lower ? String.format("%d->%d", lower, upper) : lower+"";
        }
        
       
    }
    

Log in to reply
 

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