Why do I get Segmentation Fault?


#1

I am getting segmentation fault for the below solution even though it runs fine in my compiler.

vector Solution::getRow(int A) {

vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(0);

for(int i=1;i<=A;i++){
    vector<int> temp;
    temp.push_back(0);
    
    for(int j=0;j<v.size()-1;j++){
        temp.push_back(v[j]+v[j+1]);
    }
    temp.push_back(0);
    v = temp;
}
v.erase(v.begin());
v.pop_back();

for(auto x: v){
    cout<<x<<" ";
}

}

Takes O(n^2) time complexity.


#2

You didn’t write a return v; sentence.