Accepted Java Solution


  • 0
    A

    public class Solution {

    public List<String> summaryRanges(int[] nums) {
    
        List<String> result = new ArrayList<>();
        
        int start = 0;
        int end = 0;
    
        for (int i=0; i < nums.length; i++){
            
            start = nums[i];
            
            while (i < nums.length - 1 && nums[i] + 1 == nums[i+1]){
                i++;
            }
            
            end = nums[i];
            
            //check if end is equal to start
            String range = (start == end) ? "" + start : start + "->" + end;
            
            result.add(range);
           
        }
    
        return result;
    }
    

    }


Log in to reply
 

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