class Solution { public List<String> findMissingRanges(int[] nums, int lower, int upper) { List<String> ans = new ArrayList<String>(); String tp = ""; int len = nums.length; //处理空 if(len == 0){ ans.add(Add(lower-1,upper+1)); return ans; } //处理开头 if(lower < nums[0]){ ans.add(Add( lower-1 ,nums[0])) ; } for(int i=0;i<len-1;++i){ if(nums[i]+1!=nums[i+1]){ ans.add(Add(nums[i],nums[i+1])); } } //处理结尾 if(nums[len-1]<upper){ ans.add(Add(nums[len-1],upper+1)); } return ans; } public String Add(int l,int r){ String fin = ""; if(l+2 == r){ fin = String.valueOf(l+1); } else{ fin = String.valueOf(l+1) + "->" + String.valueOf(r-1); } return fin; } }
标签:upper,ac,nums,int,lower,ans,缺失,163 From: https://www.cnblogs.com/h404nofound/p/16770674.html