Java solution, short.


  • 2
    Z
    public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> ret = new ArrayList<>();
    
        for(int start = 0, end =0; start<nums.length; ){
            end = start;
            while(end < nums.length -1 && nums[end] +1 == nums[end +1])
                end ++;
    
            // make range
            ret.add(start == end? 
                ""+nums[end] : 
                ""+nums[start]+"->"+nums[end]);
            start = end +1;
        }
    
        return ret;
    }
    

    }


Log in to reply
 

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