/***
* 执行方法
*/
class Solution {
public int[] spiralOrder(int[][] matrix) {
//空值处理
if(matrix.length == 0)
return new int[0];
//初始化
int left = 0,
right = matrix[0].length - 1,
top = 0,
buttom = matrix.length - 1,
x = 0;
int result[] = new int[(right + 1) * (buttom + 1)];
//循环打印
while(true) {
// 从左到右
for(int i = left; i <= right; i++)
{
result[x] = matrix[top][i];
x++;
}
top++;
if(top > buttom) break;
// 从上到下
for(int i = top; i <= buttom; i++)
{
result[x] = matrix[i][right];
x++;
}
right--;
if(left > right) break;
// 从右到左
for(int i = right; i >= left; i--)
{
result[x] = matrix[buttom][i];
x++;
}
buttom--;
if(top > buttom) break;
// 从下到上
for(int i = buttom; i >= top; i--)
{
result[x] = matrix[i][left];
x++;
}
left++;
if(left > right) break;
}
return result;
}
}
顺时针输出矩阵
最新推荐文章于 2022-07-04 16:36:49 发布