JAVA 10-line AC solution, beat 92%


  • 0
    A
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> res = new ArrayList<String>();
        if(nums==null || nums.length == 0) {res.add(lower==upper ? lower + "" : lower + "->" + upper); return res;}
        if(nums[0]>lower) res.add(lower + 1 == nums[0] ? lower + "" : lower + "->" + (nums[0]-1));
        for(int i=1; i<nums.length; ++i){
            if(nums[i-1]==Integer.MAX_VALUE) continue;
            if(nums[i] > nums[i-1] + 1) res.add(nums[i]==nums[i-1]+2 ? nums[i-1] + 1 +"" : nums[i-1]+1 + "->" + (nums[i]-1));   
        }
        if(nums[nums.length-1]<upper) res.add(nums[nums.length-1] + 1 == upper ? upper + "" : nums[nums.length-1] + 1  + "->" + upper);
        return res;
    }

Log in to reply
 

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