Accepted Java Solution


  • 0
    M
    public List<String> summaryRanges(int[] nums) {
        List<String> range = new ArrayList<>();
        int start = 0;
        for(int i=1;i<=nums.length;i++){
            if(i==nums.length){
                range.add(generateString(nums[start],nums[i-1], start==(i-1)?true:false));
                break;
            }
            
            if(nums[i]-nums[i-1]==1)
                continue;
            
            range.add(generateString(nums[start],nums[i-1],start==(i-1)?true:false));
            start = i;
        }
        return range;
    }
    private String generateString(int s, int e, boolean isSame){
        return (isSame)? String.valueOf(s): String.valueOf(s)+"->"+String.valueOf(e);
    }

Log in to reply
 

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