leetcode 59. Spiral Matrix II(螺旋矩阵II)

Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

Example:

Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

给出一个数字n,让输出一个二维数组,元素是从1~n2,而且要是螺旋方式

思路:
从1~n2很简单,主要是螺旋方式,螺旋方式可以参考54题,还是两条边两条边地输出,先是第一行和最后一列,然后
迂回最后一行和第一列,如此循环。

注意每个while (row <= endRow && col <= endCol) {结束时,row都是多一行或者少一行的,
而且为了避免位置重复,列也是要+1或者-1

//0ms
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        
        int element = 1;
        int lastE = n * n;
        
        int startRow = 0;
        int endRow = n - 1;
        int startCol = 0;
        int endCol = n - 1;
        
        
        int row = startRow;
        int col = startCol;
        
        while (element <= lastE) {
            while (row <= endRow && col <= endCol) {
                result[row][col] = element;
                if (col == endCol) {
                    row ++;
                } else {
                    col ++;
                }
                element ++;
            }
            row --;
            col --;
            startRow ++;
            endCol --;
            
            while (row >= startRow && col >= startCol) {
                result[row][col] = element;
                if (col == startCol) {
                    row --;
                } else {
                    col --;
                }
                element ++;
            }
            row ++;
            col ++;
            endRow --;
            startCol ++;
            
        }
        return result;
    }

另一版本

    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int sR = 0;
        int eR = n - 1;
        int sC = 0;
        int eC = n - 1;
        int row = 0;
        int col = 0;
        int i = 1;
            
        while(sR <= eR) {         
            if(row == sR) {
               while(col <= eC) {
                   res[row][col] = i ++;
                   col ++;
               }
               col = eC;
               row ++;
               while(row <= eR) {
                   res[row][col] = i ++;
                   row ++;
               }
                row = eR;
                col --;
                sR ++;
                eC --;
            }
            
            if(row == eR) {
                while(col >= sC) {
                    res[row][col] = i ++;
                    col --;
                }
                col = sC;
                row --;
                while(row >= sR) {
                    res[row][col] = i ++;
                    row --;
                }
                row =  sR;
                col ++;
                eR --;
                sC ++;
            }
        }
        return res;
    }

3

public int[][] generateMatrix(int n) {
    int[][] res = new int[n][n];
    int cnt = 1;

    int minR = 0;
    int maxR = n-1;
    int minC = 0;
    int maxC = n-1;
    int r = 0;
    int c = 0;

    while(minR <= maxR && minC <= maxC) {
        if(minR <= maxR && minC <= maxC) {
            while(c <= maxC) {
                res[r][c] = cnt++;
                c ++;
            }
            c = maxC;
            minR ++;
            r = minR;
            while(r <= maxR){
                res[r][c] = cnt++;
                r++;
            }
            r = maxR;
            maxC--;
            c = maxC;
        }
        if(minR <= maxR && minC <= maxC) {
            while(c >= minC) {
                res[r][c] = cnt++;
                c --;
            }
            c = minC;
            maxR --;
            r = maxR;
            while(r >= minR){
                res[r][c] = cnt++;
                r--;
            }
            r = minR;
            minC++;
            c = minC;
        }
    }
    return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值