Easy java solution


  • 0
    D
    public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<String>();
        if (nums.length==0) return res;
      
        String s = Integer.toString(nums[0]), e = ""; 
          
          
        for(int i=1;i<nums.length;i++){
             
             if(nums[i]-nums[i-1]==1) e =Integer.toString(nums[i]);
             else{
                 if(e=="") res.add(s);
                 else{
                     String temp = ""+s+"->"+e;
                     res.add(temp);
                 } 
                s = Integer.toString(nums[i]);
                e="";
             }
            
        }
        if(e=="") res.add(s);
        else{
                     String temp = ""+s+"->"+e;
                     res.add(temp);
        } 
                
        return res;
       }
     }

Log in to reply
 

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