classSolution { public: vector<int> spiralOrder(vector<vector<int>>& matrix){ if (matrix.size() == 0 || matrix[0].size() == 0) return {}; int left = 0, right = matrix[0].size() - 1, top = 0, bottom = matrix.size() - 1; vector<int> res; while (true) { // left to right for (int i = left; i <= right; ++i) res.push_back(matrix[top][i]); if (++top > bottom) break; // top to bottom for (int i = top; i <= bottom; ++i) res.push_back(matrix[i][right]); if (--right < left) break; // right to left for (int i = right; i >= left; --i) res.push_back(matrix[bottom][i]); if (--bottom < top) break; // bottom to top for (int i = bottom; i >= top; --i) res.push_back(matrix[i][left]); if (++left > right) break; } return res; } };