#Leetcode# 73. Set Matrix Zeroes

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

 

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

Example 1:

Input: 
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output: 
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:

Input: 
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output: 
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

代码:

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        map<int, int> mpl;
        map<int, int> mpr;
        mpl.clear();
        mpr.clear();
        int n = matrix.size(), m = matrix[0].size();
        for(int i = 0; i < n; i ++) {
            for(int j = 0; j < m; j ++) {
                if(matrix[i][j] == 0) {
                    mpr[i] = 1;
                    mpl[j] = 1;
                }
            }
        }
        for(int i = 0; i < n; i ++) {
            if(mpr[i] == 1)
                for(int j = 0; j < m; j ++)
                    matrix[i][j] = 0;
        }
        for(int i = 0; i < m; i ++) {
            if(mpl[i] == 1)
                for(int j = 0; j < n; j ++)
                    matrix[j][i] = 0;
        }
    }
};

  

posted @ 2018-11-22 10:27  丧心病狂工科女  阅读(130)  评论(0编辑  收藏  举报