https://www.acwing.com/problem/content/39/ 直接模拟
javaclass Solution {
public int[] printMatrix(int[][] matrix) {
if(matrix == null || matrix .length == 0 || matrix[0].length == 0) {
return new int[0];
}
int row = matrix.length;
int col = matrix[0].length;
int top = 0;
int bot = row - 1;
int left = 0;
int right = col - 1;
int index = 0;
int[] result = new int[row * col];
while(left <= right && top <= bot) {
for(int i = left; i <= right; i++) {
result[index++] = matrix[top][i];
}
top++;
for(int i = top; i <= bot; i++) {
result[index++] = matrix[i][right];
}
right--;
if(top <= bot) {
for(int i = right; i >= left; i--) {
result[index++] = matrix[bot][i];
}
bot--;
}
if(left <= right) {
for(int i = bot; i >= top; i--) {
result[index++] = matrix[i][left];
}
left++;
}
}
return result;
}
}
本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!