leetcode 73. Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.


class Solution {
public:
	void setZeroes(vector<vector<int>>&matrix)
	{
		if (matrix.empty()||matrix[0].empty())
			return;
		set<int>rows, cols;
		for (int i = 0; i < matrix.size(); i++)
		{
			for (int j = 0; j < matrix[0].size(); j++)
			{
				if (matrix[i][j] == 0)
				{
					rows.insert(i);
					cols.insert(j);
				}
			}
		}
		for (set<int>::iterator it = rows.begin(); it != rows.end(); it++)
			for (int j = 0; j < matrix[0].size(); j++)
				matrix[*it][j] = 0;
		for (set<int>::iterator it = cols.begin(); it != cols.end(); it++)
			for (int j = 0; j < matrix.size(); j++)
				matrix[j][*it] = 0;
	}
};

accepted

你可能感兴趣的:(LeetCode)