Open yokostan opened 5 years ago
There are so many edge cases to consider. So the best solution is to take this problem into three parts and write a formRange as a side function!
class Solution { public List<String> findMissingRanges(int[] nums, int lower, int upper) { List<String> result = new ArrayList<>(); if (nums == null || nums.length == 0){ result.add(formRange(lower,upper)); return result; } // 1st step if (nums[0] > lower){ result.add(formRange(lower,nums[0]-1)); } // 2nd step for (int i = 0; i < nums.length-1; i++){ if (nums[i+1] != nums[i] && nums[i+1] > nums[i] +1) { result.add(formRange(nums[i]+1, nums[i+1]-1)); } } // 3rd step if (nums[nums.length-1] < upper){ result.add(formRange(nums[nums.length-1]+1, upper)); } return result; } public String formRange(int low, int high){ return low == high ? String.valueOf(low) : (low + "->" + high); } }
There are so many edge cases to consider. So the best solution is to take this problem into three parts and write a formRange as a side function!