LeetCode--矩形走位

发布时间 2023-06-22 20:30:43作者: archaique

59. 螺旋矩阵 II

定义一个总数,是所有格子走完中心的最大数,target = n*n

从 1 开始,每走一步,当前数 +1 ,while(curNum<=target) 就继续走

定义每圈螺旋走位的边界,初始值:left=0; right=n-1; top=0; bottom=n-1;

  1、在顶部一行 从左到右走,从 left 走到 right

  由于占了顶部一行,top 要向下缩进一行

  2、在右边一列 从上到下走,从 top 走到 bottom

  由于占了右边一列,right 要向左缩进一列

  3、在底部一行 从右到左走,从 right 走到 left

  由于占了底部一行,bottom 要向上缩进一行

  4、在左边一列 从下到上走,从 bottom 走到 top

  由于占了左边一列,left 要向右缩进一列

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] resMat = new int[n][n];
        int target = n*n;
        int left = 0;
        int right = n-1;
        int top = 0;
        int bottom = n-1;
        int curNum = 1;
        while(curNum <= target) {
            // 在顶部一行,从左走到右
            for (int i=left;i<=right;i++) {
               // 注意这里是 [top][i]
                resMat[top][i] = curNum;
                curNum ++;
            }
            // 从左到右一行走完了,占了上边一行,top 要向下缩进一行
            top++;

            // 在右部一列,从上走到下
            for (int i=top;i<=bottom;i++) {
                // 注意这里是 [i][right]
                resMat[i][right] = curNum;
                curNum ++;
            }
             // 从上到下一列走完了,占了右边一列,right 要向左缩进一列
            right--;

            // 在底部一行,从右走到左
            for (int i=right;i>=left;i--) {
                // 注意这里是 >= left i--
                // 注意这里是 [bottom][i]
                resMat[bottom][i] = curNum;
                curNum ++;
            }
            // 从右到左一行走完了,占了下边一行,bottom 要向上缩进一行
            bottom--;

             // 在左部一列,从下走到上
            for (int i=bottom;i>=top;i--) {
                // 注意这里是 >= top i--
                // 注意这里是 [i][left]
                resMat[i][left] = curNum;
                curNum ++;
            }
             // 从下到上一列走完了,占了左边一列,left 要向右缩进一列
            left++;
        }
        return resMat;
    }
}

 

54. 螺旋矩阵

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

 

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        int length = matrix.length;
        int width = matrix[0].length;
        int sum = length*width;
        int left = 0;
        int right = width-1;
        int top = 0;
        int bottom = length-1;
        int i = 0;
        int j = 0;
        int cnt = 0;

        List<Integer> res = new ArrayList<>();
        while(cnt<sum) {
            
            // 在顶部一行
            j=top;
            //从左走到右
            for(i=left;i<=right;i++) {
                res.add(matrix[j][i]);
                //每走一步,计数+1
                cnt++;
            }
            // 在顶部一行,从左走到右,top向下
            top++;
            // 加这个的原因是,因为总数不一定是4的倍数,可能是长方形,所以不一定能走完一个螺旋,就结束了
            if (cnt==sum) {
                break;
            }

            //在右部一列
            i=right;
            //从上走到下
            // 注意这里 top 是已经++ 过的,所以这次走不到 [n,n], 但是前面在顶行走的时候,是 i<=right, 所以会走到[n,n]
            for(j=top;j<=bottom;j++) {
                res.add(matrix[j][i]);
                cnt++;
            }
            //在右部一列,从上走到下,right向左
            right--;
            if (cnt==sum) {
                break;
            }

            //在底部一行
            j=bottom;
            //从右走到左
            for(i=right;i>=left;i--) {
                res.add(matrix[j][i]);
                cnt++;
            }
            //在底部一行,从右走到左,bottom向上
            bottom--;
            if (cnt==sum) {
                break;
            }

            //在左部一列
            i=left;
            //从下走到上
            for(j=bottom;j>=top;j--) {
                res.add(matrix[j][i]);
                cnt++;
            }
            //在左部一列,从下走到上,left向右
            left++;
            if (cnt==sum) {
                break;
            }

        }
        return res;
        
    }
}