Accepted Java Solution


  • 0
    M
    public class Solution {
             public List<String> findMissingRanges(int[] nums, int lower, int upper) {
    		List<String> range = new ArrayList<String>();
    
    		for (int i = 0; i < nums.length; i++) {
    			if (nums[i] > lower) {
    				addMissingRange(lower, nums[i] - 1, range);
    			}
    			lower = nums[i] + 1;
    		}
    
    		if (upper < Integer.MAX_VALUE && lower <= upper) {
    			addMissingRange(lower, upper, range);
    		}
    
    		return range;
    	}
    
    	private void addMissingRange(int lower, int upper, List<String> range) {
    		if (lower == upper) {
    			range.add(String.valueOf(lower));
    		} else {
    			range.add(lower + "->" + upper);
    		}
    	}
    }
    

Log in to reply
 

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