Simple to understand JAVA solution


  • 1
    C

    public class Solution {
    public List<String> summaryRanges(int[] nums) {

        int n = nums.length;
        List<String> list = new ArrayList<String>();
        
        
        for(int i = 0; i < n; i++)
        {
            int first = i;
            
            while(((i + 1) < n) && (nums[i + 1] == nums[i] + 1))
            {
                i++;
            }
            
            if(i == first)
            {
                list.add(nums[i] + "");
            }
            else
            {
                list.add(nums[first] + "->" + nums[i]);
            }
        }
        
        return list;
        
    }
    

    }


Log in to reply
 

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