Solution-sharing Accepted Java solution


  • 0
     public List<String> summaryRanges(int[] nums) {
    
        List<String> result = new ArrayList<String>();
        if(nums.length<1)return result;
        int start = nums[0];
        int end = 0;
        int i = 0;
        while(i<=nums.length-1){
            if(i+1 == nums.length || nums[i+1] != nums[i]+1 ){
                end = nums[i];
                if(end == start){
                    result.add(Integer.toString(start));
                    }
                else{
                    String s = Integer.toString(start) + "->" + Integer.toString(end);
                    result.add(s);
                }
                if(i <nums.length-1)start = nums[i+1];
            }
            i++;
        }
        return result;
    }

Log in to reply
 

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