Zeroes Set Matrix
Original link: http://blog.csdn.net/doc_sgl/article/details/11834933
Given a m x n matrix, if an element was 0, set its entire row and column to 0. Do it on place.
Follow up:
Did you use extra space?
A straight forward solution using O (MN) space is probably a bad idea.
A Simple Improvement uses O (M + N) space, but still is not the best solution.
Could you devise a constant space solution?
Originally thought that the matrix as long as there is a 0, this line and this column will be set to 0, and then the entire matrix is 0. So just find out if there are 0 in the matrix. But I do not know this, only according to the original matrix 0 instead of 0 after the re-scan matrix, and the original matrix is only related to 0.
O (m + N) Space:
[CPP]
- void Setzeroes (vector<vector<int> > &matrix) {
- //Start typing your/C + + solution below
- //do not write int main () function
- int row = Matrix.size ();
- if (row = = 0) return;
- int col = matrix[0].size ();
- if (col = = 0) return;
- bool* record = new Bool[row+col];
- memset (record,false,row+col);
- For (int i = 0; i < row; ++i)
- For (int j = 0; J < col; ++j) {
- if (matrix[i][j] = = 0) {
- Record[i] = 1;
- RECORD[ROW+J] = 1;
- }
- }
- For (int i = 0; i < row; ++i)
- if (Record[i]) {
- For (int j = 0; J < col; ++j)
- MATRIX[I][J] = 0;
- }
- For (int j = row; J < Row+col; ++j)
- if (Record[j]) {
- For (int i = 0; i < row; ++i)
- Matrix[i][j-row] = 0;
- }
- }
Constant Space:
Constant space, the first can be considered is not a fixed number of variables can be done, otherwise you can consider whether the problem itself has provided enough space.
This problem belongs to the latter, which is to use the first and first columns of the matrix as the auxiliary space. No need to open up new storage space. The method is:
1. Determine if the first row and first column need to be zeroed
That is, to see if there is 0 in the first row, write it down. Also note that there is no 0 in the first column.
2. Scan the rest of the matrix elements, if you encounter 0, the corresponding first row and the first column of the element is assigned a value of 0
There is no need to worry about changing the first row or the first column of 1 to 0, because these values are destined to become 0, such as matrix[i][j]==0, then matrix[i][0] in line I, Matrix[0][j] in column J, and finally set to 0.
3. According to the information in the first and first columns, the remaining matrix elements can already be assigned the value required for the result, i.e., take the first behavior example, if scanned to a 0, the column is cleared 0.
4. Process the first and first columns according to the status determined in 1.
If you start with 0 in the first row, the line is zeroed. Handle the column similarly.
[CPP]
- void Setzeroes (vector<vector<int> > &matrix) {
- //Start typing your/C + + solution below
- //do not write int main () function
- int row = Matrix.size ();
- if (row = = 0) return;
- int col = matrix[0].size ();
- if (col = = 0) return;
- bool Firstrowiszero = false;
- bool Firstcoliszero = false;
- For (int j = 0; J < col; ++j)
- if (matrix[0][j] = = 0) {
- Firstrowiszero = true;
- Break ;
- }
- For (int i = 0; i < row; ++i)
- if (matrix[i][0] = = 0) {
- Firstcoliszero = true;
- Break ;
- }
- For (int i = 1; i < row; ++i)
- For (int j = 1; j < col; ++j) {
- if (matrix[i][j] = = 0) {
- Matrix[i][0] = 0;
- MATRIX[0][J] = 0;
- }
- }
- For (int i = 1; i < row; ++i)
- For (int j = 1; j < col; ++j)
- if (matrix[i][0] = = 0 | | matrix[0][j] = = 0)
- MATRIX[I][J] = 0;
- if (Firstrowiszero) {
- For (int j = 0; J < col; ++j)
- MATRIX[0][J] = 0;
- }
- if (Firstcoliszero) {
- For (int i = 0; i < row; ++i)
- Matrix[i][0] = 0;
- }
- }
Set Matrix Zeroes