Very Concise Java Solution


  • 0
        public List<String> summaryRanges(int[] nums) {
            List<String> res = new ArrayList<>();
            int i = 0;
            while (i < nums.length) {
                int j = i + 1;
                while (j < nums.length && nums[j] == nums[j - 1] + 1) j++;
                if (j == i + 1) res.add(String.valueOf(nums[i]));
                else res.add(nums[i] + "->" + nums[j - 1]);
                i = j;
            }
            return res;
        }
    

Log in to reply
 

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