228. Summary Ranges (everyday promlems) broken problems

Given a sorted integer array without duplicates, return the summary of its ranges.

Example 1:

Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.

Example 2:

Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.

broken solution : check the boundary

class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        int n = nums.length;
        if(n==0) return res;
        else if(n==1) {
            res.add(nums[0]+"");
            return res;
        }
        int i = 0;
        while(i<n-1){
            //case for only n-1
            if(nums[i] +1 == nums[i+1] ){ //
                int start = nums[i];
                i = i+1;
                
                while(nums[i] +1 == nums[i+1]){//i+1<n
                   
                    i++;
                }
                int end = nums[i];
                String str = start + "->" + end;
                res.add(str);
            }
            else if(nums[i] +1 != nums[i+1] ){
                res.add(nums[i]+"");}
            i++;
        }
        //check the last element
        if(nums[n-1] == nums[n-2]+1) {
            if(n>=3){
                String[] temp = res.get(res.size()-1).split("->");
                res.remove(res.size()-1);
                res.add(temp[0] + "->" + nums[n-1]);
            }else {
                //n ==2 
                res.add(nums[n-2] + "->" + nums[n-1]);
            }
        }else {
            res.add(nums[n-1]+"");
        }
        return res;
    }
}

revision correct one: always check the boundary

two cases: 1: 1,2,3  2: [1,2,4,5,7]

class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        int n = nums.length;
        if(n==0) return res;
        else if(n==1) {
            res.add(nums[0]+"");
            return res;
        }
        int i = 1;
        while(i<=n-1){
            //case for only n-1
            if(nums[i-1] +1 == nums[i] ){ //
                int start = nums[i-1];
                i = i+1;
                
                while(i<n && nums[i-1] +1 == nums[i]){//i+1<n
                    i++;
                }
                int end = nums[i-1];
                String str = start + "->" + end;
                res.add(str);
            }
            else if(nums[i-1] +1 != nums[i] ){
                res.add(nums[i-1]+"");}
            i++;
        }
        //check the last element
        if(nums[n-1] == nums[n-2]+1) {
           
        }else {
            res.add(nums[n-1]+"");
        }
        return res;
    }
}

two pointer solution

class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        int n = nums.length;
        if(n==0) return res;
        else if(n==1) {
            res.add(nums[0]+"");
            return res;
        }
        //use two pointers
        int i = 0;int j = 0;//i and j
        while(i<n){
            j = i+1;
            while(j < n){
                if(nums[j-1]+1 == nums[j]){
                    j++;
                }else {
                    break;
                }
            }
            if(j == i+1){
                res.add(nums[i]+"");
            }else {
                res.add(nums[i]+"->"+nums[j-1]);
            }
            i = j;
        }
        return res;
    }
}

ren ruoyousuopcheng, biypusuozhi

猜你喜欢

转载自www.cnblogs.com/stiles/p/leetcode228.html