classSolution{publicint[][]generateMatrix(int n){int l =0, r = n -1, t =0, b = n -1;int[][] mat =newint[n][n];int num =1, tar = n * n;while(num <= tar){for(int i = l; i <= r; i++){
mat[t][i]= num++;}
t++;for(int i = t; i <= b; i++){
mat[i][r]= num++;}
r--;for(int i = r; i >= l; i--){
mat[b][i]= num++;}
b--;for(int i = b; i >= t; i--){
mat[i][l]= num++;}
l++;}return mat;}}
classSolution{publicint[]spiralOrder(int[][] matrix){if(matrix.length ==0){returnnewint[0];}int l =0, r = matrix[0].length -1, t =0, b = matrix.length -1, x =0;int[] res =newint[(r +1)*(b +1)];while(true){for(int i = l; i <= r; i++){
res[x++]= matrix[t][i];}if(++t > b)break;for(int i = t; i <= b; i++){
res[x++]= matrix[i][r];}if(l >--r)break;for(int i = r; i >= l; i--){
res[x++]= matrix[b][i];}if(t >--b)break;for(int i = b; i >= t; i--){
res[x++]= matrix[i][l];}if(++l > r)break;}return res;}}
classSolution{publicList<Integer>spiralOrder(int[][] matrix){List<Integer> res =newArrayList<>();int l =0, r = matrix[0].length -1, t =0, b = matrix.length -1;while(true){for(int i = l; i <= r; i++){
res.add(matrix[t][i]);}if(++t > b)break;for(int i = t; i <= b; i++){
res.add(matrix[i][r]);}if(l >--r)break;for(int i = r; i >= l; i--){
res.add(matrix[b][i]);}if(t >--b)break;for(int i = b; i >= t; i--){
res.add(matrix[i][l]);}if(++l > r)break;}return res;}}
59. 螺旋矩阵 Ⅱclass Solution { public int[][] generateMatrix(int n) { int l = 0, r = n - 1, t = 0, b = n - 1; int[][] mat = new int[n][n]; int num = 1, tar = n * n; while(num <= tar){ for(int i = l; i <.