【LeetCode】59. Spiral Matrix II 解题报告


转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51416284


Subject

出处:https://leetcode.com/problems/spiral-matrix/

Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.

For example, Given n = 3,
You should return the following matrix:

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

Explain

螺旋遍历往一个n*n的数组中填入数字。方向是→、↓、←、↑,依次循环。


My Solution

使用螺旋遍历输出数组的方法,填入数字即可,数字自增。

参考:http://blog.csdn.net/crazy1235/article/details/51416037


public static int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];

        int top = 0;
        int right = 0;
        int bottom = 0;
        int left = 0;
        int index = 1;
        int i = 0;
        while (true) {
            // top
            for (i = left; i < n - right; i++) {
                result[top][i] = index++;
            }
            top++;
            if (top + bottom == n) {
                break;
            }
            // right
            for (i = top; i < n - bottom; i++) {
                result[i][n - 1 - right] = index++;
            }
            right++;
            if (left + right == n) {
                break;
            }
            // bottom
            for (i = n - 1 - right; i >= left; i--) {
                result[n - 1 - bottom][i] = index++;
            }
            bottom++;
            if (top + bottom == n) {
                break;
            }
            // left
            for (i = n - 1 - bottom; i >= top; i--) {
                result[i][left] = index++;
            }
            left++;
            if (left + right == n) {
                break;
            }
        }
        return result;
    }


由于是n * n的数组,所以只需要判断top + bottom 或者left + right即可。将判断条件放到while中。

public static int[][] generateMatrix2(int n) {
        int[][] result = new int[n][n];

        int topOffset = 0;
        int rightOffset = 0;
        int bottomOffset = 0;
        int leftOffset = 0;
        int index = 1;

        int i = 0;

        while (topOffset + bottomOffset < n) {
            // top
            for (i = leftOffset; i < n - rightOffset; i++) {
                result[topOffset][i] = index++;
            }
            topOffset++;
            // right
            for (i = topOffset; i < n - bottomOffset; i++) {
                result[i][n - 1 - rightOffset] = index++;
            }
            rightOffset++;
            // bottom
            for (i = n - 1 - rightOffset; i >= leftOffset; i--) {
                result[n - 1 - bottomOffset][i] = index++;
            }
            bottomOffset++;
            // left
            for (i = n - 1 - bottomOffset; i >= topOffset; i--) {
                result[i][leftOffset] = index++;
            }
            leftOffset++;
        }

        return result;
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值