来源:Leetcode
原帖:http://oj.leetcode.com/problems/spiral-matrix/
题目:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
代码:
原帖:http://oj.leetcode.com/problems/spiral-matrix/
题目:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
代码:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
if (matrix.empty() || matrix[0].empty()) return {};
vector<int> res;
int imin = 0, imax = matrix.size() - 1; // row
int jmin = 0, jmax = matrix[0].size() - 1; // col
while (1) {
for (int j = jmin; j <= jmax; ++j) res.push_back(matrix[imin][j]);
if (++imin > imax) break;
for (int i = imin; i <= imax; ++i) res.push_back(matrix[i][jmax]);
if (jmin > --jmax) break;
for (int j = jmax; j >= jmin; --j) res.push_back(matrix[imax][j]);
if (imin > --imax) break;
for (int i = imax; i >= imin; --i) res.push_back(matrix[i][jmin]);
if (++jmin > jmax) break;
}
return res;
}
};
No comments:
Post a Comment