FYI java solution


  • 0
    C
    public List<String> summaryRanges(int[] nums) {
        List<String> result = new ArrayList<>();
        
        for (int first = 0, i = 1; i <= nums.length; i++) {
            if (i == nums.length || nums[i] > nums[i - 1] + 1) {
                StringBuilder range = new StringBuilder();
                range.append(nums[first]);
                if (first != i - 1) range.append("->" + nums[i - 1]);
                result.add(range.toString());
                first = i;
            }
        }
        return result;
    }

Log in to reply
 

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