My 1ms Java Solution


  • 0
    T
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if( nums.length == 0 ){
            return res;
        }
        int start = Integer.MIN_VALUE, end = Integer.MIN_VALUE;
        String tmp = "";
        for( int i = 0; i < nums.length;  ){
            if( tmp == "" ){
                start = nums[i];
                end = nums[i];
                i++;
                tmp += start;
            }else if( nums[i] == end + 1 ){
                end = nums[i];
                i++;
            }else{
                if( start == end ){
                    res.add(tmp); 
                }else{
                    tmp += "->" + end;
                    res.add(tmp);
                }
                tmp = "";
            }
        }
        if( start != end ){
            tmp += "->" + end;
        }
        res.add(tmp);
        return res;
    }

Log in to reply
 

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