Easy and Concise Two Pointers Java Solution


  • 0
    public class Solution {
        public List<String> summaryRanges(int[] nums) {
            if (nums == null || nums.length == 0) return new ArrayList<>();
            List<String> res = new ArrayList<>();
            int start = nums[0], end = nums[0];
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] - end == 1) end = nums[i];
                else {
                    res.add(start == end ? start + "" : start + "->" + end);
                    start = nums[i];
                    end = nums[i];
                }
            }
            res.add(start == end ? start + "" : start + "->" + end);
            return res;
        }
    }
    

Log in to reply
 

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