Sliding window method using Java


  • 0
    C
    public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<String>();
        if(nums.length == 0) return list;
        int start = 0, end = 0;
        while(start <= end && end < nums.length){
            String tmp = "";
            if(end < nums.length - 1 && nums[end + 1] == nums[end] + 1){
                end++;
            }
            else {
                tmp += end != start ? String.valueOf(nums[start]) + "->" + String.valueOf(nums[end]) : String.valueOf(nums[start]);
                list.add(tmp);
                start = end + 1;
                end = start;
            }
        }
        return list;
    }
    

    }


Log in to reply
 

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