Need more testcase


  • 0
    R

    this code pass the judge, but fail on test case [2,5,6,7,50,52] lower:3 upper 51
    wrong output:[3->1, 3->4, 8->49, 51]
    should be: 3->4, 8->49, 51

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

  • 0

    @readman Please read the description carefully:

    Given a sorted integer array where the range of elements are [lower, upper] inclusive.


Log in to reply
 

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