vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int>* ret = new vector<int>();
int rotate = 0;
int x = 0, y = 0, w = matrix[0].size(), h = matrix.size();
while (true)
{
if (rotate == 0)
{
for (int i = x; i < w; i++)
{
cout << matrix[y][i] << " ";
ret->push_back(matrix[y][i]);
}
y += 1;
rotate = 1;
}
else if (rotate == 1)
{
for (int i = y; i < h; i++)
{
cout << matrix[i][w - 1] << " ";
ret->push_back(matrix[i][w - 1]);
}
w -= 1;
rotate = 2;
}
else if (rotate == 2)
{
for (int i = (w - 1); i >= x; i--)
{
cout << matrix[h - 1][i] << " ";
ret->push_back(matrix[h - 1][i]);
}
h -= 1;
rotate = 3;
}
else if (rotate == 3)
{
for (int i = (h - 1); i >= y; i--)
{
cout << matrix[i][x] << " ";
ret->push_back(matrix[i][x]);
}
x += 1;
rotate = 0;
}
if (x == w || y == h)
break;
}
return *ret;
}
顺时针打印矩阵
最新推荐文章于 2024-08-29 22:59:49 发布