Simple Java Solution


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

Log in to reply
 

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