标签:ace end row mat in place 需要 tor its etc
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
这题太水了,根本不是medium难度,一遍就AC了。
遍历matrix,拿两个数组分别记录需要变成0的行和列就OK了。
下面是AC的代码:
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
vector<int> row, col;
int m = matrix.size();
if(m == 0){
return ;
}
int n = matrix[0].size();
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(matrix[i][j] == 0){
row.push_back(i);
col.push_back(j);
}
}
}
for(vector<int>::iterator iter = row.begin(); iter != row.end(); iter++){
for(int i = 0; i < n; i++){
matrix[*iter][i] = 0;
}
}
for(vector<int>::iterator iter = col.begin(); iter != col.end(); iter++){
for(int i = 0; i < m; i++){
matrix[i][*iter] = 0;
}
}
}
};
113
LeetCode OJ 73. Set Matrix Zeroes
标签:ace end row mat in place 需要 tor its etc
原文地址:https://www.cnblogs.com/YuNanlong/p/8802038.html