My clean Java solution


  • 0
    V
    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> res = new ArrayList<>();
            int prev, cur;
            for(int i = 0; i <= nums.length; i++) {
                cur = i == nums.length ? upper + 1: nums[i];
                prev = i > 0 ? nums[i - 1] : lower - 1;
                
                if (cur > prev + 1) {
                    res.add(getRange(prev, cur));
                }
                
                prev = cur;
            }
            
            return res;
        }
        
        private String getRange(int prev, int cur) {
            if (cur - prev == 2) {
                return String.valueOf(cur - 1);
            } else {
                return String.valueOf(prev + 1) + "->" + String.valueOf(cur - 1);
            }
        }
    }

  • 0
    S
    This post is deleted!

Log in to reply
 

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