very easy solution


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

Log in to reply
 

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