Bug report, if upper is smaller than some number in array nums


  • 0
    J

    First, post my code.
    public class Solution {

    public List<String> findMissingRanges(int[] nums, int lower, int upper) {

        List<String> res = new ArrayList<String>();
        
        if (nums == null || nums.length == 0) {
            if (lower == upper) {
                res.add(lower + "");
            } else {
                res.add(lower + "->" + upper);
            }
            return res;
        }
        
        int last = lower;
        for (int i = 0; i < nums.length && nums[i] <= upper; i++) {
                if (nums[i] == last) {
                    last++;
                } else if (nums[i] > last) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(last);
                    last++;
                    if (nums[i] > last) {
                        sb.append("->").append(nums[i] - 1);
                    }
                    last = nums[i] + 1;
                    res.add(sb.toString());
                }
        }
        
        if (last == upper) {
            res.add(upper + "");
        } else if (last < upper) {
            res.add(last + "->" + upper);
        }
        
        return res;
    }
    

    }

    If we give this test case,
    [0,1,3,50,75] with lower 0 and upper 67.

    I think the result should be ["2","4->49","51->67"]
    but the expected answer shows me
    ["2","4->49","51->74"]


Log in to reply
 

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