# leetcode #73 in cpp

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;

}
}

• 本文已收录于以下专栏：

## Leetcode 73. Set Matrix Zeroes (Medium) (cpp)

Leetcode 73. Set Matrix Zeroes (Medium) (cpp)
• Niko_Ke
• 2016年07月27日 21:21
• 645

## leetcode #114 in cpp

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 ...

## leetcode #160 in cpp

Write a program to find the node at which the intersection of two singly linked lists begins. F...

## Leetcode 285. Inorder Successor in BST (Medium) (cpp)

Leetcode 285. Inorder Successor in BST (Medium) (cpp)
• Niko_Ke
• 2016年12月28日 03:38
• 242

## leetcode #139 in cpp

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa...

## leetcode #36 in cpp.

The question is to determine a Sudoku is valid.  Solution:  if a Sudoku is valid, each row, each co...

## *leetcode 138 in cpp

A linked list is given such that each node contains an additional random pointer which could point t...

## leetcode #17 in cpp

The question is : Given a digit string, return all possible letter combinations that the number coul...

## leetcode \$42 in cpp

Code: (This method is not the best one. There are other methods using two pointers) Key: a bar coul...

## *leetcode #80 in cpp

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, G...

举报原因： 您举报文章：leetcode #73 in cpp 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)