Java, Easy to read


  • 0
    Z
    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> ret = new ArrayList<>();
            
            if(nums== null || nums.length ==0){
                ret.add(makeString(lower,upper));
                return ret;
            }
            
            for(int n : nums){
                if (n > upper)
                    break;
                    
                if(lower > n)
                    continue;
                else if(lower < n){
                    ret.add(makeString(lower,n -1));
                    lower = n + 1;
                }
                else{ // lower == n 
                    lower = n + 1;
                }
            }
            
            int last = nums[nums.length -1];
            if(upper > last)
                ret.add(makeString(last +1, upper));
            
            return ret;
        }
        
        private String makeString(int s, int e){
            return s == e ? 
                ""+ s: 
                String.format("%d->%d", s, e);
        }
    }

Log in to reply
 

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