Nasty Solution Passes all Corner Cases


  • 0
    Y

    public class Solution {
    public List<String> findMissingRanges(int[] A, int lower, int upper) {
    List<String> result = new ArrayList<String>();
    if (A.length == 0){
    if (lower == upper){
    result.add("" + lower);
    }else{
    result.add(lower + "->" + upper);
    }
    return result;
    }
    if (lower == upper)
    return result;

        int pre;
        if (lower == Integer.MIN_VALUE){
            pre = lower;
        }else{
            pre = lower - 1;
        }
        for(int i = 0; i <= A.length; i++){
            int after;
            if (i == A.length){
                if (upper == Integer.MAX_VALUE){
                    after = upper;
                }else{
                    after = upper + 1; 
                }
            }else{
                after = A[i];
            }
            if (after == Integer.MIN_VALUE || after == Integer.MAX_VALUE && i < A.length - 1)
                continue;
            if (pre == Integer.MIN_VALUE && lower == Integer.MIN_VALUE && i == 0){
                if (pre == after - 1){
                    result.add(String.valueOf(pre));
                }else if (pre < after - 1){
                    result.add(String.valueOf(pre) + "->" + String.valueOf(after - 1));
                }
            }else if (after == Integer.MAX_VALUE && upper == Integer.MAX_VALUE && i == A.length){
                if (pre == after - 1){
                    result.add(String.valueOf(after));
                }else if (pre < after - 1){
                    result.add(String.valueOf(pre + 1) + "->" + String.valueOf(after));
                }
            }else {
                if(pre + 2 == after){
                    result.add(String.valueOf(pre + 1));
                }else if(pre + 2 < after){
                    result.add(String.valueOf(pre + 1) + "->" + String.valueOf(after - 1));
                }
            }
            pre = after;
        }
        return result;
    }
    

    }


Log in to reply
 

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