Java accepted solution


  • 0
    W
    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> missingRange = new ArrayList<>();
            long pre = lower;
            for (int i = 0; i < nums.length; i++) {
                if ((long) nums[i] - 1 == pre) {
                    missingRange.add((nums[i] - 1) + "");
                } else if ((long) nums[i] - 1 > pre) {
                    missingRange.add(pre  + "->" + (nums[i] - 1));
                }
                pre = (long) nums[i] + 1;
            }
            if (upper == pre) {
                missingRange.add(upper + "");
            } else if (upper > pre) {
                missingRange.add(pre  + "->" + upper);
            }
            return missingRange;
        }
    }

Log in to reply
 

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