solution.cpp 790 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <bits/stdc++.h>
using namespace std;
public:
void setZeroes(vector<vector<int>> &matrix)
{
	bool bRow = false, bCol = false;
	for (int row = 0; row < matrix.size(); row++)
	{
		for (int col = 0; col < matrix[row].size(); col++)
		{
			if (matrix[row][col] == 0)
			{
				if (row == 0)
				{
					bRow = true;
				}
				if (col == 0)
				{
					bCol = true;
				}
				matrix[0][col] = matrix[row][0] = 0;
			}
		}
	}
	for (int row = 1; row < matrix.size(); row++)
	{
		for (int col = 1; col < matrix[row].size(); col++)
		{
			if (matrix[0][col] == 0 || matrix[row][0] == 0)
			{
				matrix[row][col] = 0;
			}
		}
	}
	if (bRow)
	{
		for (auto &m : matrix[0])
		{
			m = 0;
		}
	}
	if (bCol)
	{
		for (int row = 0; row < matrix.size(); row++)
		{
			matrix[row][0] = 0;
		}
	}
}
}
;