Easy to follow Java solution


  • 0
    B
    public class Solution {
    public List<String> summaryRanges(int[] array) {
        List<String> summary = new LinkedList<String>();
        if (array.length == 0) return summary;
        
        int startIndex = 0, fwdIndex = 0, endIndex = 0;
        StringBuilder summ = new StringBuilder();
        
        while(startIndex < array.length){
            fwdIndex = startIndex + 1;
            while (fwdIndex < array.length && array[fwdIndex]  == array[fwdIndex - 1] + 1 ){
                ++fwdIndex;
            }
            endIndex = fwdIndex - 1;
            summ.setLength(0);
            if (startIndex == endIndex){
                summ.append(array[startIndex]);
                summary.add(summ.toString());
            }else{
                summ.append(array[startIndex] + "->" + array[endIndex]);
                summary.add(summ.toString());
            }
            startIndex = fwdIndex;
        }
        return summary;
        
    }
    

    }


Log in to reply
 

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