# leetcode #73 in cpp

61人阅读 评论(0)

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

Solution 1: O(m+n) space.

We can use two arrays to record which row and which column should be set zero. Once we know all of them, we can start to set 0.

Code:

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if(matrix.empty()) return;

vector<int> row_zero;
vector<int> col_zero;

for(int i = 0; i < matrix.size(); i ++){
for(int j = 0; j < matrix[0].size(); j ++){
if(matrix[i][j] == 0){//record which row and which column should be set 0
row_zero.push_back(i);
col_zero.push_back(j);
}
}
}
for(int i = 0; i < row_zero.size(); i ++){//set row to 0
for(int j = 0; j < matrix[0].size(); j ++)
matrix[row_zero[i]][j] = 0;
}
for(int j = 0; j < col_zero.size(); j++){//set column = 0
for(int i = 0; i < matrix.size(); i ++){
matrix[i][col_zero[j]] = 0;
}
}
}
};

Solution 2: recursion, O(m+n) space

In each recursion, we find next 0 that is not found in previous recursion. Once we find it, we go to next recursion to search other 0s. When next recursion ends, we set the corresponding row and column to zeros and go back to previous recursion. In this way the location is retained while we could perform next search.

Code:

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
setZeros2(matrix,0,0);
}
void setZeros2(vector<vector<int>>& matrix,int row, int col){//search by row

while(row < matrix.size()){//start looking for 0 from position (row,col)
while(col<matrix[0].size()){
if(matrix[row][col] == 0) {
break;
}
col++;
}
if(col!=matrix[0].size()) break;//means we have found 1 0
row ++;
col = 0;
}
if(row>=matrix.size()) return;//if we dont find 0 any more, end this recurrence and go back to previous recurrence.

//set next starting point for searching next 0
setZeros2(matrix, col+1 == matrix[0].size()?row+1:row, (col+1)%matrix[0].size());//go to next search

//when next search ends its setting 0, set 0 for current recurrence
for(int i = 0; i < matrix.size(); i ++){
matrix[i][col] = 0;
}

for(int i = 0; i < matrix[0].size(); i ++){
matrix[row][i] = 0;
}
return;

}
};

Solution 3: constant space

When we find a 0 at position(i,j), we know that ith row and jth column should be zero. To record this, we set mat[0][j] = 0 and mat[0][i] = 0. In this way, the first row and the first column serves as a flag. We also have to use 2 flags to record if our first row and first column should be set 0. This is because during finding 0s, the first row and column is changed as they are flags. We need to record if they are originally supposed to be 0.

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
setZeros_constant_space(matrix);
}
void setZeros_constant_space(vector<vector<int>>& matrix){//search by row
if(matrix.empty()) return;

bool firstRowZero = false;
bool firstColZero = false;
int m = matrix.size();
int n = matrix[0].size();

for(int i = 0; i < m; i++){//see if the first column should be set 0
if(matrix[i][0] == 0) firstColZero = true;
}
for(int i = 0; i < n; i++){//see if the first row should be set 0
if(matrix[0][i] == 0) firstRowZero = true;
}

int row = 1;
int col = 1;

while(row<m){

while(col < n){//find 0. once a 0 is found, project it to the first row and first column. set those two positions to 0
if(matrix[row][col] == 0){
matrix[0][col] = 0;
matrix[row][0] = 0;
}
col++;
}

col = 1;
row++;
}

//set 0
for(int i = 1; i < m; i ++){//if mat[i][0] = 0, it means the ith row should be zero.
if(matrix[i][0] == 0)
for(int j = 0; j < n; j ++)
matrix[i][j] = 0;
}

for(int i = 1; i < n; i ++){//if mat[0][i] = 0, it means ith column should be zero
if(matrix[0][i] == 0)
for(int j = 0; j < m; j ++)
matrix[j][i] = 0;
}
if(firstRowZero)//if first row should be zero, set it 0
for(int j = 0; j < n; j ++)
matrix[0][j] = 0;
if(firstColZero)//if first column should be zero, set it 0
for(int j = 0; j < m; j ++)
matrix[j][0] = 0;

}
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：8001次
• 积分：1395
• 等级：
• 排名：千里之外
• 原创：139篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
文章存档
阅读排行
评论排行