Why I failed this test case?


  • 0
    Z

    Input:
    [-2147483648,-2147483647,2147483647]

    Output:
    ["-2147483648->2147483647"]

    Expected:
    ["-2147483648->-2147483647","2147483647"]

    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> result;
            int i=0;
            cout<<nums.size();
            for(int j=0;i<nums.size();j++){
                cout<<'1';
                if(i==(nums.size()-1)){
                        //i is the last element
                        string temp=to_string(nums[i]);
                        result.push_back(temp);
                        break;
                }
                if((nums[j]-nums[i])>(j-i)){
                    string temp;
                    if((j-i)==1){
                        temp=to_string(nums[i]);
                    }
                    else{
                        temp=to_string(nums[i])+"->"+to_string(nums[j-1]);
                    }
                    
                    
                    result.push_back(temp);
                    i=j;
                    
                    
                }
                else if(j==(nums.size()-1)){
                    
                    string temp=to_string(nums[i])+"->"+to_string(nums[j]);
                    result.push_back(temp);
                    i=j+1;
                }
            }
        
            return result;
        }
    };

  • 0
    A

    got the same problem. Can someone tell us why the input is of type int, but still can have test numbers out of the int scope?


  • 0
    C

    if((nums[j]-nums[i])>(j-i)) , this line was the problem.

    when nums[i]=2147483647,nums[i-1]=-2147483648,nums[i]-nums[i-1]>Integer.MAX_VALUE.

    you shoule change it to if(nums[i] > nums[i-1] + 1)


Log in to reply
 

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