题解 | #螺旋矩阵#
螺旋矩阵
https://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
using System;
using System.Collections.Generic;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param matrix int整型二维数组
* @return int整型一维数组
*/
public List<int> spiralOrder (List<List<int>> matrix) {
List<int> res = new List<int>();
int n = matrix.Count;
if(n == 0) return res;
int m = matrix[0].Count;
int left = 0, right = m - 1;
int top = 0, bottom = n - 1;
while(res.Count < m*n){
for(int i = left; i <= right; i++){
res.Add(matrix[top][i]);
}
top++;
for(int j = top; j <= bottom; j++){
res.Add(matrix[j][right]);
}
right--;
if(res.Count == m*n) break;
for(int i = right; i >= left; i--){
res.Add(matrix[bottom][i]);
}
bottom--;
for(int j = bottom; j >= top; j--){
res.Add(matrix[j][left]);
}
left++;
}
return res;
}
}
网易游戏公司福利 651人发布