Problem statement : Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
My code:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> p;
vector<int> k;
p.push_back(1);
k[nums.size()-1]=1;
for(int i=1;i<nums.size();i++){
p[i]=p[i-1]*nums[i-1];
}
for(int i=nums.size()-2;i>=0;i--){
k[i]=k[i+1]*nums[i+1];
}
for(int i=0;i<nums.size();i++){
nums[i]=p[i]*k[i];
}
return nums;
}
And getting this error:
Line 1034: Char 34: runtime error: applying non-zero offset 12 to null pointer (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34
Aucun commentaire:
Enregistrer un commentaire