class Solution { public: void setZeroes(vector<vector<int> > &matrix) { int rows = matrix.size(); int cols = matrix[0].size(); bool col_has_zero = false; bool row_has_zero = false; for (int i=0; i<cols; i++) { if (matrix[0][i] == 0) { row_has_zero = true; break; } } for (int i=0; i<rows; i++) { if (matrix[i][0] == 0) { col_has_zero = true; break; } } for (int i=1; i<rows; i++) { for (int j=1; j<cols; j++) { if (matrix[i][j] != 0) continue; matrix[i][0] = 0; matrix[0][j] = 0; } } for (int i=1; i<rows; i++) { for (int j=1; j<cols; j++) { if (!matrix[i][0] || !matrix[0][j]) { matrix[i][j] = 0; } } } for (int i=0; row_has_zero && i<cols; i++) matrix[0][i] = 0; for (int i=0; col_has_zero && i<rows; i++) matrix[i][0] = 0; } };
没意思,搞个几个bit的额外空间会死么
LeetCode Set Matrix Zeroes,布布扣,bubuko.com
原文:http://www.cnblogs.com/lailailai/p/3853695.html