Accepted Java solution 8 lines & 0ms


  • 15
    M
    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> res = new ArrayList<>();
            for(int i : nums) {
                if(i > lower) res.add(lower+((i-1 > lower)?"->"+(i-1):""));
                if(i == upper) return res; // Avoid overflow
                lower = i+1;
            }
            if(lower <= upper) res.add(lower + ((upper > lower)?"->"+(upper):""));
            return res;
        }
    }
    

    Just 8 lines but still very readable.


  • 1
    E

    Excellent! This should be on the top!


  • 0
    F
    This post is deleted!

  • 0
    D

    @mcopes Your solution is the only one got accepted.


Log in to reply
 

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