公司网站建设的普遍性,google排名,天津市区县档案部门网站建设指导意见,wordpress 描述代码LCR 146题
题目描述#xff1a; 给定一个二维数组 array#xff0c;请返回「螺旋遍历」该数组的结果。 螺旋遍历#xff1a;从左上角开始#xff0c;按照 向右、向下、向左、向上 的顺序 依次 提取元素#xff0c;然后再进入内部一层重复相同的步骤#xff0c;直到提取完…LCR 146题
题目描述 给定一个二维数组 array请返回「螺旋遍历」该数组的结果。 螺旋遍历从左上角开始按照 向右、向下、向左、向上 的顺序 依次 提取元素然后再进入内部一层重复相同的步骤直到提取完所有元素。 **题解思路**这道题和力扣54题一样题解见https://blog.csdn.net/Miss_croal/article/details/141180630?spm1001.2014.3001.5502唯一不一样的就是边界这个会有 0 的情况所以一定要把判断放到最前面 class Solution {public int[] spiralArray(int[][] array) {// 样例中有一维空数组一定要放到最前面来判断否则会显示下标越界if(array.length 0) return new int[0];int m array.length;int n array[0].length;int[] res new int[m * n];int i 0, j 0, x 0, y 0, index 0, offset 1;int loop 0;while (loop Math.min(m, n) / 2) {// 向右for (j y; j n - offset; j) {res[index] array[x][j];}// 向下for (i x; i m - offset; i) {res[index] array[i][j];}// 向左for (; j y; j--) {res[index] array[i][j];}// 向上for (; i x; i--) {res[index] array[i][j];}// 更新x;y;offset;loop;}// 判断最后要添加的是中间行还是中间列if (Math.min(m, n) % 2 1) {if (m n) {// 添加中间列for (int t 0; t (m - n 1); t) {res[index] array[x][y];}} else {// 添加中间行for (int t 0; t (n - m 1); t) {res[index] array[x][y];}}}return res;}
}