Simple Java solution beats 99.14%


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

  • 1
    W

    It's wrong!!!!!

    Input:
    [2147483647]
    0
    2147483647
    Output:
    ["0->2147483646","-2147483648->2147483647"]
    Expected:
    ["0->2147483646"]


Log in to reply
 

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