classSolution { public: voidsetZeroes(vector<vector<int>>& matrix){ int m = matrix.size(); int n = matrix[0].size(); vector<bool> rowFlag(m, false); vector<bool> colFlag(n, false);
for (int i = 0; i < m; i ++) { for (int j = 0; j < n; j ++) { if (!matrix[i][j]) { rowFlag[i] = true; colFlag[j] = true; } } }
for (int i = 0; i < m; i ++) { for (int j = 0; j < n; j ++) { if (rowFlag[i] || colFlag[j]) { matrix[i][j] = 0; } } } } };
classSolution { public: vector<int> spiralOrder(vector<vector<int>>& matrix){ int m = matrix.size(); int n = matrix[0].size();
int left = 0, right = n-1, top = 0, bottom = m-1; vector<int> ans(m * n, 0); int idx = 0; while (left <= right && top <= bottom) { for (int i = left; i <= right; i ++) { ans[idx++] = matrix[top][i]; }
for (int i = top+1; i <= bottom; i ++) { ans[idx++] = matrix[i][right]; }
if (top < bottom) { for (int i = right-1; i >= left; i --) { ans[idx++] = matrix[bottom][i]; } }
if (left < right) { for (int i = bottom-1; i > top; i --) { ans[idx++] = matrix[i][left]; } }
left ++; right --; top ++; bottom --; } return ans; } };
classSolution { public: voidrotate(vector<vector<int>>& matrix){ int n = matrix.size(); int start = 0, end = n - 1; while (start <= end) { for (int i = start; i < end; i ++) { int temp = matrix[start][i]; matrix[start][i] = matrix[n - i - 1][start]; matrix[n - i - 1][start] = matrix[end][n - i - 1]; matrix[end][n - i - 1] = matrix[i][end]; matrix[i][end] = temp; } start ++; end --; } } };