您的位置:首页 > 其它

leetcode 73. Set Matrix Zeroes

2016-04-16 21:36 344 查看

题意

将矩阵中为0点它所在行和列都设置为0

题解

用两个集合保存需要设置的行和列。时间复杂度为O(m * n)

代码

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
set<int> rows, cols;
int m = matrix.size(), n = matrix[0].size();
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrix[i][j] == 0)
rows.insert(i), cols.insert(j);
}

}

for(set<int>::iterator i = rows.begin(); i != rows.end(); i++)
{
for(int j = 0; j < n; j++)
{
matrix[*i][j] = 0;
}

}
for(set<int>::iterator i = cols.begin(); i != cols.end(); i++)
{
for(int j = 0; j < m; j++)
{
matrix[j][*i] = 0;
}

}

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: