Runtime error on test case [1,2]


  • 0
    P

    Runtime error on [1,2]
    No error in VS 2013. What happened... Please help.

    void nextPermutation(vector<int>& nums) {
        if(nums.size()==0)
            return;
        int i = nums.size()-1;
        
        while(i>=1 && nums[i]<=nums[i-1])
            i--;
        
        if(i==(nums.size()-1))
        {
            swap(nums[i],nums[i-1]);
        }else if(i==0)
        {
            int j = 0, k = nums.size()-1;
            while(j<k)
            {
                swap(nums[j],nums[k]);
                j++;
                k--;
            }
        }else
        {
            int j=nums.size()-1;
            for(j=nums.size()-1;j>=i;j--)
            {
                if(nums[j]>nums[i-1])
                    break;
            }
            swap(nums[i-1],nums[j]);
            
            j = i;
            int k = nums.size()-1;
            while(j<k)
            {
                swap(nums[j],nums[k]);
                j++;
                k--;
            }
        }
        return;
    }

  • 1

    Somehow the judge shows the wrong input. You actually don't fail [1,2] but [1]. You can see it yourself by adding

    cout << nums.size() << " numbers:";
    for (int i : nums)
        cout << ' ' << i;
    cout << endl;
    

    at the the start of the function.

    I think you'll easily see why you fail that case.


Log in to reply
 

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