0ms JAVA solution


  • 0
    D

    public class Solution {
    public List<String> summaryRanges(int[] nums) {
    List<String> ls = new ArrayList<String>();
    if(nums.length==0) return ls;
    if(nums.length==1)
    {
    ls.add(String.valueOf(nums[0]));
    return ls;
    }

        int p1=nums[0];
        String s ="";
        for(int i =0;i<nums.length-1;i++)
        {
            if(nums[i+1]-nums[i]!=1)
            {
                if(p1!=nums[i])
                {
                     s = String.valueOf(p1)+"->"+String.valueOf(nums[i]);
                }
                else
                {
                      s = String.valueOf(p1);
                }
               
                p1=nums[i+1];
                ls.add(s);
                 if(i==nums.length-2) {String p=String.valueOf(nums[i+1]); ls.add(p);}
            }
          else
          {
              if(i+1==nums.length-1)
              {
                   s =String.valueOf(p1)+"->"+String.valueOf(nums[i+1]);
              
              ls.add(s);}
          }
        }
        return ls;
        
    }
    

    }


Log in to reply
 

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