163. Missing Ranges

Back to Homepage   |     Back to Code List


class Solution {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        long l = (long) lower;
        long u = (long) upper;
        List<String> res = new ArrayList<>();
        int n = nums.length;

        if (n == 0) {
            add(res, l - 1, u + 1);
            return res;
        }

        add(res, l - 1, nums[0]);
        for (int i = 1; i < n; i++) {
            add(res, nums[i - 1], nums[i]);
        }
        add(res, nums[n - 1], u + 1);
        return res;
    }

    private void add(List<String> res, long lo, long hi) {
        if (lo == hi) {
            return;
        } else if (lo + 1 == hi) {
            return;
        } else if (lo + 1 == hi - 1) {
            res.add(String.valueOf(lo + 1));
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(String.valueOf(lo + 1));
            sb.append("->");
            sb.append(String.valueOf(hi - 1));
            res.add(sb.toString());
        }
    }
}