Java solution in 15 lines


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

Log in to reply
 

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