Java Simple Solution O(N)


  • 0
    J
    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> res = new ArrayList<> ();
            int start = lower;
            
            for(int i=0;i<nums.length;i++){
                if(nums[i]!=start) {
                    if(nums[i]-1==start) res.add(Integer.toString(start));
                    else res.add(start+"->"+(nums[i]-1));
                }
                start = nums[i]+1;
            }
            
            if(start<upper+1) {
                if(start==upper) res.add(start+"");
                else res.add(start+"->"+upper);
            }
            return res;
        }
    }
    

Log in to reply
 

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