Easy to Understand JAVA solution, very concise and clean coding.


  • 1
    M
    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> res = new ArrayList<>();
            int from = lower - 1;
            int to = upper + 1;
            for(int num : nums) {
                to = num;
                addMissingRangesToList(res, from, to);
                from = num;
            }
            addMissingRangesToList(res, from, upper+1);
            return res;
        }
        
        private void addMissingRangesToList(List<String> list, int from, int to) {
            if(from + 1 >= to) return;
            if(from + 2 == to) list.add(from + 1 + "");
            else {
                list.add((from + 1) + "->" + (to - 1));
            }
        }
    }
    

  • 0
    K

    @Michael_Facebooker until now, You are the only one I saw who could pass all the test cases.


  • 0
    M

    I guess it wont pass when lower=0, upper=5, array={10,11,12}


Log in to reply
 

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