代码拉取完成,页面将自动刷新
// Time: O(m * n)
// Space: O(1)
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if (matrix.empty()) {
return res;
}
for (int left = 0, right = matrix[0].size() - 1,
top = 0, bottom = matrix.size() - 1;
left <= right && top <= bottom;
++left, --right, ++top, --bottom) {
for (int j = left; j <= right; ++j) {
res.emplace_back(matrix[top][j]);
}
for (int i = top + 1; i < bottom; ++i) {
res.emplace_back(matrix[i][right]);
}
for (int j = right; top < bottom && j >= left; --j) {
res.emplace_back(matrix[bottom][j]);
}
for (int i = bottom - 1; left < right && i > top; --i) {
res.emplace_back(matrix[i][left]);
}
}
return res;
}
};
// Time: O(m * n)
// Space: O(1)
class Solution2 {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
const int m = matrix.size();
vector<int> res;
if (m == 0) {
return res;
}
const int n = matrix.front().size();
enum Action {RIGHT, DOWN, LEFT, UP};
Action action = RIGHT;
for (int i = 0, j = 0, begini = 0, beginj = 0, endi = m,
endj = n, cnt = 0, total = m * n; cnt < total; ++cnt) {
res.emplace_back(matrix[i][j]);
switch (action) {
case RIGHT:
if (j + 1 < endj) {
++j;
} else {
action = DOWN, ++begini, ++i;
}
break;
case DOWN:
if (i + 1 < endi) {
++i;
} else {
action = LEFT, --endj, --j;
}
break;
case LEFT:
if (j - 1 >= beginj) {
--j;
} else {
action = UP, --endi, --i;
}
break;
case UP:
if (i - 1 >= begini) {
--i;
} else {
action = RIGHT, ++beginj, ++j;
}
break;
default:
break;
}
}
return res;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。