Java solution for beginners. Very easy to understand.


  • 0
    P

    The main part is to increase the low value to the current value + 1. rest will follow. Look out for few edge cases.

     public List<String> findMissingRanges(int[] nums, int lower, int upper) {
    
    		List<String> res = new ArrayList<>();
    
    		for (int i = 0; i < nums.length; i++) {
    			if (nums[i] > lower) {
    				if (lower == nums[i] - 1)
    					res.add("" + lower);
    				else
    					res.add(lower + "->" + (nums[i] - 1));
    			}
    			lower = nums[i] + 1;
    		}
    
    		if (upper >= lower) { // Corner stuffs.
    			if (lower == upper)
    				res.add("" + lower);
    			else
    				res.add(lower + "->" + upper);
    		}
    		return res;
    	}
    

Log in to reply
 

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