|
| 1 | +给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 |
| 2 | + |
| 3 | +``` |
| 4 | +示例 1: |
| 5 | +输入: |
| 6 | +[ |
| 7 | + [1,1,1], |
| 8 | + [1,0,1], |
| 9 | + [1,1,1] |
| 10 | +] |
| 11 | +输出: |
| 12 | +[ |
| 13 | + [1,0,1], |
| 14 | + [0,0,0], |
| 15 | + [1,0,1] |
| 16 | +] |
| 17 | +示例 2: |
| 18 | +
|
| 19 | +输入: |
| 20 | +[ |
| 21 | + [0,1,2,0], |
| 22 | + [3,4,5,2], |
| 23 | + [1,3,1,5] |
| 24 | +] |
| 25 | +输出: |
| 26 | +[ |
| 27 | + [0,0,0,0], |
| 28 | + [0,4,5,0], |
| 29 | + [0,3,1,0] |
| 30 | +] |
| 31 | +``` |
| 32 | + |
| 33 | +### 进阶: |
| 34 | +一个直接的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。 |
| 35 | + |
| 36 | +一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。 |
| 37 | + |
| 38 | +你能想出一个常数空间的解决方案吗? |
| 39 | + |
| 40 | +### 思路1 |
| 41 | + |
| 42 | +1. 创建行列辅助数组,先遍历矩阵,定位元素是0的行列,分别加入行数组,列数组 |
| 43 | +2. 从行数组中取出元素,整行置零;同理列数组 |
| 44 | + |
| 45 | +空间复杂度`O(m+n)`,而且分别进行处理和列处理时有重复操作 |
| 46 | + |
| 47 | +### 思路2(优化思路1) |
| 48 | + |
| 49 | +1. 先检查首行首列有没有0,有的话设置bool标记 |
| 50 | +2. 从第二行第二列开始遍历,如果发现有0,设置`matrix[i][0] = 0和matrix[0][j] = 0`,即把首行首列对应行列值设置为0 |
| 51 | +3. 遍历首行首列,把值为0的行按行设置为0;列同理 |
| 52 | +4. 查看标记位,看是否需要把首行首列设置为0 |
| 53 | + |
| 54 | +这种思路没有用额外的空间,但是时间复杂度和思路1一样,都有待解决重复操作的问题 |
| 55 | + |
| 56 | +整体好于思路1,时间复杂度比思路1稳定 |
| 57 | + |
| 58 | +### Solution1 |
| 59 | + |
| 60 | +```CPP |
| 61 | +class Solution { |
| 62 | +public: |
| 63 | + void setZeroes(vector<vector<int>>& matrix) { |
| 64 | + if(matrix.empty())return; |
| 65 | + //行数组,列数组 |
| 66 | + int rowNum = matrix.size(); |
| 67 | + int columnNum = matrix[0].size(); |
| 68 | + vector<int> rowVec; |
| 69 | + vector<int> columnVec; |
| 70 | + |
| 71 | + |
| 72 | + for(int i = 0;i<rowNum;i++){ |
| 73 | + for(int j = 0;j<columnNum;j++){ |
| 74 | + if(matrix[i][j] == 0){ |
| 75 | + auto iter = find(rowVec.begin(),rowVec.end(),i); |
| 76 | + if(iter == rowVec.end())rowVec.push_back(i); |
| 77 | + |
| 78 | + iter = find(columnVec.begin(),columnVec.end(),j); |
| 79 | + if(iter == columnVec.end())columnVec.push_back(j); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + rowNum = rowVec.size(); |
| 85 | + if(rowNum == 0)return; |
| 86 | + |
| 87 | + int row; |
| 88 | + int column; |
| 89 | + |
| 90 | + //行处理 |
| 91 | + for(int i = 0;i<rowNum;i++){ |
| 92 | + row = rowVec[i]; |
| 93 | + for(int j = 0;j<columnNum;j++){ |
| 94 | + matrix[row][j] = 0; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + //列处理 |
| 99 | + columnNum = columnVec.size(); |
| 100 | + rowNum = matrix.size(); |
| 101 | + for(int i = 0 ; i < columnNum;i++){ |
| 102 | + column = columnVec[i]; |
| 103 | + for(int j = 0;j<rowNum;j++){ |
| 104 | + matrix[j][column] = 0; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | +}; |
| 110 | +``` |
| 111 | +
|
| 112 | +### Solution 2 |
| 113 | +
|
| 114 | +```CPP |
| 115 | +class Solution { |
| 116 | +public: |
| 117 | + void setZeroes(vector<vector<int>>& matrix) { |
| 118 | + if(matrix.empty()) return; |
| 119 | + int m = matrix.size(); |
| 120 | + int n = matrix[0].size(); |
| 121 | + bool row = false , column = false; |
| 122 | + |
| 123 | + |
| 124 | + for(int i = 0; i < m; i++)//判断第1列的0; |
| 125 | + { |
| 126 | + if(matrix[i][0] == 0) |
| 127 | + { |
| 128 | + column = true; |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + for(int i = 0; i < n; i ++)//判断第1行的0; |
| 133 | + { |
| 134 | + if(matrix[0][i] == 0) |
| 135 | + { |
| 136 | + row = true; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + for(int i = 1; i < m;i++) |
| 142 | + { |
| 143 | + for(int j = 1; j < n;j++) |
| 144 | + { |
| 145 | + if(matrix[i][j] == 0) |
| 146 | + { |
| 147 | + matrix[0][j] = 0; |
| 148 | + matrix[i][0] = 0; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + for(int i = 1; i < m;i++) |
| 153 | + { |
| 154 | + for(int j = 1; j < n;j++) |
| 155 | + { |
| 156 | + if(matrix[i][0] == 0 || matrix[0][j] == 0) |
| 157 | + { |
| 158 | + matrix[i][j] = 0; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + if(row) |
| 163 | + for(int i = 0; i < n;i++) |
| 164 | + matrix[0][i] = 0; |
| 165 | + if(column) |
| 166 | + for(int i = 0; i < m;i++) |
| 167 | + matrix[i][0] = 0; |
| 168 | + |
| 169 | + return; |
| 170 | +
|
| 171 | + |
| 172 | + } |
| 173 | +}; |
| 174 | +``` |
0 commit comments