# C++ Solution hitting "runtime error", but ran fine locally on the example that it failed on.

• The code ran through. I am puzzled where the runtime error came from. I compiled and used a main() function to test with the same input matrix and it worked fine. Any ideas?

``````#include <vector>
#include <iostream>

using namespace std;

class Solution {
// for constant space solution
// use a row that contains 0 to store columns that contain zeros
public:
void setZeroes(vector<vector<int> >& matrix) {
int m = -1;
for (size_t i=0; i<matrix.size(); ++i) {
bool foundzero = false;
for (size_t j=0; j<matrix[i].size(); ++j) {
if (matrix[i][j] == 0) {
foundzero = true;
break;
}
}
if (foundzero && m<0) { // first time a 0 is found
m = i; // row to be used as column markers
for (size_t j=0; j<matrix[m].size(); ++j) {
if (matrix[m][j]!=0) matrix[m][j] = -1;
else matrix[m][j] = 1;
}
continue;
}
if (foundzero) {
// zero out whole row while marking columns
for (size_t j=0; j<matrix[j].size(); ++j) {
if (matrix[i][j]==0) matrix[m][j] = 1;
matrix[i][j] = 0;
}
}
}
if (m>=0) {
// now zero out the columns except for marker row
for (size_t j=0; j<matrix[m].size(); ++j) {
if (matrix[m][j]<0) continue;
for (size_t i=0; i<matrix.size(); ++i) {
if (i!=m) matrix[i][j] = 0;
}
}
// finally zero out the marker row
for (size_t j=0; j<matrix[m].size(); ++j) matrix[m][j] = 0;
}
}
};

int main (void) {
vector<vector<int> > input;
int a[4] = {1,2,3,4};
int b[4] = {5,0,5,2};
int c[4] = {8,9,2,0};
int d[4] = {5,7,2,1};
input.push_back(vector<int>(a, a+4));
input.push_back(vector<int>(b, b+4));
input.push_back(vector<int>(c, c+4));
input.push_back(vector<int>(d, d+4));
Solution sol;
sol.setZeroes(input);
for (size_t i=0; i<input.size(); ++i) {
for (size_t j=0; j<input[i].size(); ++j) cout <<input[i][j]<<",";
cout << endl;
}
return 0;
}``````

• What you've done is excellent, except 1 line: //old: j<matrix[j].size() . I changed it to make the code works
One NOTE: when there is runtime error, leetcode only show last executed input, it may not show the actual input that caused the error.

``````class Solution {

public:
void setZeroes(vector<vector<int>>& matrix) {
int m = -1;
for (size_t i=0; i<matrix.size(); ++i) {
bool foundzero = false;
for (size_t j=0; j<matrix[i].size(); ++j) {
if (matrix[i][j] == 0) {
foundzero = true;
break;
}
}
if (foundzero && m<0) { // first time a 0 is found
m = i; // row to be used as column markers
for (size_t j=0; j<matrix[m].size(); ++j) {
if (matrix[m][j]!=0) matrix[m][j] = -1;
else matrix[m][j] = 1;
}
continue;
}
if (foundzero) {
// zero out whole row while marking columns
for (size_t j=0; j<matrix[i].size(); ++j) { //old: j<matrix[j].size()
if (matrix[i][j]==0) matrix[m][j] = 1;
matrix[i][j] = 0;
}
}
}
if (m>=0) {
// now zero out the columns except for marker row
for (size_t j=0; j<matrix[m].size(); ++j) {
if (matrix[m][j]<0) continue;
for (size_t i=0; i<matrix.size(); ++i) {
if (i!=m) matrix[i][j] = 0;
}
}
// finally zero out the marker row
for (size_t j=0; j<matrix[m].size(); ++j) matrix[m][j] = 0;
}
}
};``````

• Thank you! That was a typo.
I don't understand how come the last executed input wouldn't be the one that caused the error. But it's good to know.

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