clear solution


  • 0
    W
    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> result = new ArrayList<String>(); 
            if (nums == null) nums = new int[0]; 
            long current = lower; 
            for (int i = 0; i < nums.length; i++) {
               // System.out.println(current);
                if (current > upper) break; 
                if (current > nums[i]) continue; 
                if (current == nums[i]) current++; 
                else { // nums[i]-1 >= current; 
                    addRange(result, current, nums[i]-1);
                    current = nums[i]+1L; 
                }
            }
            if (current <= (long)upper) addRange(result, current, upper); 
            return result;
        }
        private void addRange(List<String> result, long left, long right) {
            if (left == right) result.add("" + left); 
            else {
                String range = "" + left + "->" + right; 
                result.add(range); 
            }
        }
    }
    

Log in to reply
 

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