Set Matrix Zeroes

Source: Internet
Author: User

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]
  1. void Setzeroes (vector<vector<int> > &matrix) {
  2. //Start typing your/C + + solution below
  3. //do not write int main () function
  4. int row = Matrix.size ();
  5. if (row = = 0) return;
  6. int col = matrix[0].size ();
  7. if (col = = 0) return;
  8. bool* record = new Bool[row+col];
  9. memset (record,false,row+col);
  10. For (int i = 0; i < row; ++i)
  11. For (int j = 0; J < col; ++j) {
  12. if (matrix[i][j] = = 0) {
  13. Record[i] = 1;
  14. RECORD[ROW+J] = 1;
  15. }
  16. }
  17. For (int i = 0; i < row; ++i)
  18. if (Record[i]) {
  19. For (int j = 0; J < col; ++j)
  20. MATRIX[I][J] = 0;
  21. }
  22. For (int j = row; J < Row+col; ++j)
  23. if (Record[j]) {
  24. For (int i = 0; i < row; ++i)
  25. Matrix[i][j-row] = 0;
  26. }
  27. }

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]
  1. void Setzeroes (vector<vector<int> > &matrix) {
  2. //Start typing your/C + + solution below
  3. //do not write int main () function
  4. int row = Matrix.size ();
  5. if (row = = 0) return;
  6. int col = matrix[0].size ();
  7. if (col = = 0) return;
  8. bool Firstrowiszero = false;
  9. bool Firstcoliszero = false;
  10. For (int j = 0; J < col; ++j)
  11. if (matrix[0][j] = = 0) {
  12. Firstrowiszero = true;
  13. Break ;
  14. }
  15. For (int i = 0; i < row; ++i)
  16. if (matrix[i][0] = = 0) {
  17. Firstcoliszero = true;
  18. Break ;
  19. }
  20. For (int i = 1; i < row; ++i)
  21. For (int j = 1; j < col; ++j) {
  22. if (matrix[i][j] = = 0) {
  23. Matrix[i][0] = 0;
  24. MATRIX[0][J] = 0;
  25. }
  26. }
  27. For (int i = 1; i < row; ++i)
  28. For (int j = 1; j < col; ++j)
  29. if (matrix[i][0] = = 0 | | matrix[0][j] = = 0)
  30. MATRIX[I][J] = 0;
  31. if (Firstrowiszero) {
  32. For (int j = 0; J < col; ++j)
  33. MATRIX[0][J] = 0;
  34. }
  35. if (Firstcoliszero) {
  36. For (int i = 0; i < row; ++i)
  37. Matrix[i][0] = 0;
  38. }
  39. }

Set Matrix Zeroes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.