题意是要求在01矩阵中,把0的点的行和列都置零。
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
void unguarded_setZero(int *matrix, int m, int n, int i, int j)
{
for (int ii = 0; ii < m; ++ii)
{
*(matrix + ii * n + j) = 0;
}
for (int jj = 0; jj < n; ++jj)
{
*(matrix + i * n + jj) = 0;
}
}
void solve(int *matrix, int m, int n)
{
if (matrix == NULL || m <= 0 || n <= 0)
{
return;
}
vector<pair<int, int> > vecZero;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (*(matrix + i * n + j) == 0)
{
vecZero.push_back(make_pair(i, j));
}
}
}
for (vector<pair<int, int> >::const_iterator it = vecZero.begin();
it != vecZero.end();
++it)
{
const int i = it->first;
const int j = it->second;
unguarded_setZero(matrix, m, n, i, j);
}
}
int main(int argc, char **argv)
{
int matrix[][6] =
{
{1, 1, 0, 1, 1, 1},
{0, 1, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 1}
};
solve((int *)matrix, 3, 6);
for (size_t i = 0; i < sizeof(matrix) / sizeof(*matrix); ++i)
{
for (size_t j = 0; j < sizeof(*matrix) / sizeof(**matrix); ++j)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
这次要求O(1)的空间复杂度。
void setZeroes(vector<vector<int> > &matrix)
{
bool bColZero = false, bRowZero = false;
if (matrix.size() == 0 || matrix[0].size() == 0)
{
return;
}
// Mark bColZero true when col[0] contains zero.
for (size_t row = 0; row < matrix.size(); ++row)
{
if (!matrix[row][0]) bColZero = true;
}
// Mark bRowZero true when row[0] contains zero.
for (size_t col = 0; col < matrix[0].size(); ++col)
{
if (!matrix[0][col]) bRowZero = true;
}
// Map zero points to row[0] and col[0].
for (size_t row = 1; row < matrix.size(); ++row)
{
for (size_t col = 1; col < matrix[row].size(); ++col)
{
if (!matrix[row][col])
{
matrix[0][col] = 0;
matrix[row][0] = 0;
}
}
}
// Set zero according to row[0] and col[0].
for (size_t row = 1; row < matrix.size(); ++row)
{
for (size_t col = 1; col < matrix[row].size(); ++col)
{
if (!matrix[row][0] || !matrix[0][col])
{
matrix[row][col] = 0;
}
}
}
// Process col[0].
if (bColZero)
{
for (size_t row = 0; row < matrix.size(); ++row)
{
matrix[row][0] = 0;
}
}
// Process row[0].
if (bRowZero)
{
for (size_t col = 0; col < matrix[0].size(); ++col)
{
matrix[0][col] = 0;
}
}
}
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
void unguarded_setZero(int *matrix, int m, int n, int i, int j)
{
for (int ii = 0; ii < m; ++ii)
{
*(matrix + ii * n + j) = 0;
}
for (int jj = 0; jj < n; ++jj)
{
*(matrix + i * n + jj) = 0;
}
}
void solve(int *matrix, int m, int n)
{
if (matrix == NULL || m <= 0 || n <= 0)
{
return;
}
vector<pair<int, int> > vecZero;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (*(matrix + i * n + j) == 0)
{
vecZero.push_back(make_pair(i, j));
}
}
}
for (vector<pair<int, int> >::const_iterator it = vecZero.begin();
it != vecZero.end();
++it)
{
const int i = it->first;
const int j = it->second;
unguarded_setZero(matrix, m, n, i, j);
}
}
int main(int argc, char **argv)
{
int matrix[][6] =
{
{1, 1, 0, 1, 1, 1},
{0, 1, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 1}
};
solve((int *)matrix, 3, 6);
for (size_t i = 0; i < sizeof(matrix) / sizeof(*matrix); ++i)
{
for (size_t j = 0; j < sizeof(*matrix) / sizeof(**matrix); ++j)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
这次要求O(1)的空间复杂度。
void setZeroes(vector<vector<int> > &matrix)
{
bool bColZero = false, bRowZero = false;
if (matrix.size() == 0 || matrix[0].size() == 0)
{
return;
}
// Mark bColZero true when col[0] contains zero.
for (size_t row = 0; row < matrix.size(); ++row)
{
if (!matrix[row][0]) bColZero = true;
}
// Mark bRowZero true when row[0] contains zero.
for (size_t col = 0; col < matrix[0].size(); ++col)
{
if (!matrix[0][col]) bRowZero = true;
}
// Map zero points to row[0] and col[0].
for (size_t row = 1; row < matrix.size(); ++row)
{
for (size_t col = 1; col < matrix[row].size(); ++col)
{
if (!matrix[row][col])
{
matrix[0][col] = 0;
matrix[row][0] = 0;
}
}
}
// Set zero according to row[0] and col[0].
for (size_t row = 1; row < matrix.size(); ++row)
{
for (size_t col = 1; col < matrix[row].size(); ++col)
{
if (!matrix[row][0] || !matrix[0][col])
{
matrix[row][col] = 0;
}
}
}
// Process col[0].
if (bColZero)
{
for (size_t row = 0; row < matrix.size(); ++row)
{
matrix[row][0] = 0;
}
}
// Process row[0].
if (bRowZero)
{
for (size_t col = 0; col < matrix[0].size(); ++col)
{
matrix[0][col] = 0;
}
}
}