leetcode_set_matrix_zeroes 发表于 2016-12-05 难度:Medium 解题思路:找出应该设置为0的行和列,然后操作。 1234567891011121314151617181920212223242526272829303132class Solution {public: void setZeroes(vector<vector<int>>& matrix) { int m = matrix.size(); int n = matrix[0].size(); set<int> rows; set<int> cols; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(matrix[i][j] == 0) { rows.insert(i); cols.insert(j); } } } auto rows_it = rows.begin(); for(; rows_it != rows.end(); rows_it++) { for( int j = 0; j < n; j++) matrix[*rows_it][j] = 0; } auto cols_it = cols.begin(); for(; cols_it != cols.end(); cols_it++) { for(int i = 0; i < m; i++) matrix[i][*cols_it] = 0; } }}; 运行结果:66ms,超过20.51%