I have tried a problem in leetcode IDE. Got a runtime error on running the below code.
As I am a beginner, I'm not able to debug the error.
class Solution
{
public:
vector<int> runningSum(vector<int>& nums) {
vector<int> result;
int n=nums.size();
for(int i=0;i<n;i++)
{
int sum=0;
for(int j=0;j<=i;j++)
{
sum=sum + nums[j];
}
result[i]=sum;
}
return result;
}
};
resultis empty. Any indexing into it will be out of bounds and lead to undefined behavior.