0 ms easy to understand Java solution


  • 0
        public List<String> summaryRanges(int[] nums) {
    //Creating result List 
           List<String> resultList = new ArrayList<String>();
            if(nums.length == 0 ){
                return resultList;
            }
            else{
            int start = nums[0],val=nums[0];
            for(int i=1;i<nums.length;i++){
                 if(nums[i]-val == 1 ){
                 }else{
                          if(start != val){
                                resultList.add(start+"->"+val);
                            }else{
                                resultList.add(Integer.toString(val)); 
                            }
                    start =nums[i];
                 }
                 val = nums[i];
            }
            /*Adding the last value*/
             if(start-val == 1 || start != val){
                                resultList.add(start+"->"+val);
                            }else{
                                resultList.add(Integer.toString(val)); 
                            }
            
        
            }
            return resultList;
        }
    }

Log in to reply
 

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