[Leetcode] 73. Set-matrix-zeroes (simulation) [medium]

link

https://leetcode-cn.com/problems/set-matrix-zeroes/

time consuming

Problem solving: 33 min
Problem solving: 18 min

Title

Given an mxn matrix, if an element is 0, set all the elements in its row and column to 0. Please use the in-place algorithm.

Advanced:

  • An intuitive solution is to use O(mn) extra space, but this is not a good solution.
  • A simple improvement is to use O(m + n) extra space, but this is still not the best solution.
  • Can you think of a solution that uses only constant space?

prompt:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • − 2 31 -2^{31} 231 <= matrix[i][j] <= 2 31 − 1 2^{31}-1 2311

Ideas

Take the first row and the first column as the mark position, that is, a certain position [i][j] is 0, then mark the first element of this row and the first element of this column as 0. Then traverse the first row and the first column, and set all the columns with 0 elements in the first row to 0, and set all the rows with 0 elements in the first column to 0.

Details: Because the first row and the first column are marked positions, the above operations cannot be performed in the first row and the first column, and special processing is required. Before the operation, first determine whether there is 0 in the first row and the first column, and mark them separately. After the above operation is over, judge the other marks of the first row and the first column before, and see if the first row and first column are set to zero based on this.

Time complexity: O (m ∗ n) O(m*n)O ( mn)

AC code

class Solution {
    
    
public:
    void setZeroes(vector<vector<int>>& matrix) {
    
    
        int m = matrix.size();
        int n = matrix[0].size();
        
        bool flag_0r = false, flag_0c = false;
        for(int i = 0; i < m; ++i) {
    
    
            if(matrix[i][0] == 0) {
    
    
                flag_0c = true;
                break;
            }
        }
        for(int j = 0; j < n; ++j) {
    
    
            if(matrix[0][j] == 0) {
    
    
                flag_0r = true;
                break;
            }
        }

        for(int i = 1; i < m; ++i) {
    
    
            for(int j = 1; j < n; ++j) {
    
    
                if(matrix[i][j] == 0) {
    
    
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        
        for(int i = 1; i < m; ++i) {
    
    
            if(matrix[i][0] != 0) continue;
            for(int j = 0; j < n; ++j) {
    
    
                matrix[i][j] = 0;
            }
        }
        for(int j = 1; j < n; ++j) {
    
    
            if(matrix[0][j] != 0) continue;
            for(int i = 0; i < m; ++i) {
    
    
                matrix[i][j] = 0;
            }
        }
        if(flag_0c) {
    
    
            for(int i = 0; i < m; ++i) matrix[i][0] = 0;
        }
        if(flag_0r) {
    
    
            for(int j = 0; j < n; ++j) matrix[0][j] = 0;
        }
    }
};

Guess you like

Origin blog.csdn.net/Krone_/article/details/115045380