Easy understand java simple code


  • 0
    M

    Easy understand java simple code:

    public class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> res = new ArrayList<>();
            int prev = lower-1;
            for (int ele: nums) {
                addRange(res, prev, ele);
                prev = ele;
            }
            addRange(res, prev, upper+1);
            return res;
        }
        
        private void addRange(List<String>res, int lower, int upper) {
            if (upper - lower <= 1) return;
            else if (upper - lower == 2) res.add(Integer.toString(lower+1));
            else res.add(String.format("%d->%d", lower+1, upper-1));
        }
    }
    

Log in to reply
 

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