Why I am getting runtime error for a particular tesr case?


  • 0
    M

    class Solution {
    public:
    vector<int> productExceptSelf(vector<int>& nums) {

        vector<int>prod={1};
        
        int size=nums.size();
      int temp;
       temp=1;
        for(int i=0;i<size;i++)
        {
            prod[i]=temp;
            temp=nums[i]*temp;
        }
        temp=1;
        for(int i=size-1;i>=0;i--)
        {
            prod[i]*=temp;
            temp=temp*nums[i];
            
        }
        
    return prod;
        
    }
    

    };


  • 0
    C

    it is better to post with the test case. I guess it is related to the way how you initialize vector prod


  • 0
    W

    Replace

    vector<int> prod={1};
    

    with

    vector<int> prod(nums.size() , 1);
    

    and try again:)


Log in to reply
 

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