Finally, passing all 34 test cases


  • 0

    I find some topics with most votes can't pass all the test cases.

    So, i wrote my own answer. Little ugly but work.

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

Log in to reply
 

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