算法2
(暴力枚举) $O(n^2)$
如果认可我的想法,请点个赞呗(我脸皮厚,嘿嘿嘿)
java 代码
class Solution {
public int[] printMatrix(int[][] matrix) {
if(matrix.length==0){
int[] love=new int[0];
return love;
}
int x=0,y=0,w1=matrix[0].length,w2=matrix.length,w3=0,w4=0;
int m=0,pd=520,m1=0;
int[] love=new int[matrix[0].length*matrix.length];
while (true) {
pd=520;
if (m==0) {
while (x < w1) {
if (m1<love.length) {
love[m1] = matrix[y][x];
}
m1++;
pd=1314;
x++;
}
if (pd!=1314){
break;
}
w1--;
y++;
x--;
m=1;
}
pd=520;
if (m==1) {
while (y < w2) {
if (m1<love.length) {
love[m1] = matrix[y][x];
}
m1++;
pd=1314;
y++;
}
if (pd!=1314){
break;
}
w2--;
m=2;
y--;
x--;
}
pd=520;
if (m==2) {
while (x >= w3) {
pd=1314;
if (m1<love.length) {
love[m1] = matrix[y][x];
}
m1++;
x--;
}
if (pd!=1314){
break;
}
w3++;
x++;
y--;
m=3;
}
pd=520;
if (m==3) {
while (y > w4) {
pd=1314;
if (m1<love.length) {
love[m1] = matrix[y][x];
}
m1++;
y--;
}
if (pd!=1314){
break;
}
w4++;
y++;
x++;
m=0;
}
}
return love;
}
}
6