Java easy to understand


  • -1
    N

    public static List<String> summaryRanges(int[] nums) {
    List<String> result=new ArrayList<String>();
    if(nums.length==0) return result;
    int start=0;
    for(int i=0;i<nums.length;i++)
    {
    String list=new String();
    if(i==nums.length-1)
    {
    if(start==i) list=nums[i]+"";
    else list=nums[start]+"->"+nums[i];
    result.add(list);
    return result;
    }
    if(nums[i+1]-nums[i]==1) continue;
    else
    {
    if(i!=start)
    {
    list=nums[start]+"->"+nums[i];

        	    }
        		else
        		{
        			list=nums[i]+"";
        			
        		}   
        	start=i+1;
        	}        	
            result.add(list);		
        }
        return result;
    }

Log in to reply
 

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