59. Spiral Matrix II

原创 2018年04月17日 21:10:39

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 ]
]

题意:

蛇形填数

思路:

因为是从左上角顺时针开始填,所以设置top,left,down,right四个变量,开始遍历,直到left==right||down==top,注意循环过程中,四个变量的变化。

代码:

JavaScript代码:

/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function(n) {
    var number=[];
    for(var i=0;i<n;i++)
        number.push([]);
    var top=0,left=0,down=n-1,right=n-1,k=1;
    while(top<=down)
        {
            for(var i=left;i<=right;i++)
                number[top][i]=k++;
            top++;
            for(var i=top;i<=down;i++)
                number[i][right]=k++;
            right--;
            for(var i=right;i>=left;i--)
                number[down][i]=k++;
            down--;
            for(var i=down;i>=top;i--)
                number[i][left]=k++;
            left++;
        }
    return number;
};

Java代码:

class Solution {
    public int[][] generateMatrix(int n) {
        if(n==0)
            return null;
        int [][]spiral=new int[n][n];
        int k=0;
        int left=0,top=0,down=n-1,right=n-1;
        while(top<=down)
        {
            for(int i=left;i<=right;i++)
                spiral[top][i]=++k;
            top++;
            for(int j=top;j<=down;j++)
                spiral[j][right]=++k;
            right--;
            for(int i=right;i>=left;i--)
                spiral[down][i]=++k;
            down--;
            for(int j=down;j>=top;j--)
                spiral[j][left]=++k;
            left++;
        }
        return spiral;
    }
}


59. Spiral Matrix II Leetcode Python

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
  • hyperbolechi
  • hyperbolechi
  • 2015-02-03 23:48:58
  • 715

leetcode_c++: Spiral Matrix II(059)

题目Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-06-01 00:28:33
  • 125

【LeetCode with Python】 Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For...
  • nerv3x3
  • nerv3x3
  • 2014-11-30 21:50:32
  • 12036

LeetCode54/59 Spiral Matrix I/II

一:Spiral Matrix I 题目: Given a matrix of m x n elements (m rows, n columns), return all elements ...
  • Lu597203933
  • Lu597203933
  • 2015-04-06 20:23:18
  • 1674

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

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51416037Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-05-15 11:21:35
  • 3575

LeetCode59——Spiral Matrix II

LeetCode59——Spiral Matrix II 参考LeetCode54——Spiral Matrix 唯一不同,就是对二维数组按照这个顺序进行赋值操作:1,2,3,....n 代码:...
  • zhangxiao93
  • zhangxiao93
  • 2015-10-24 22:13:19
  • 684

[LeetCode]59.Spiral Matrix II

【题目】 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral orde...
  • SunnyYoona
  • SunnyYoona
  • 2015-02-04 22:25:02
  • 1383

LeetCode | 59. Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For e...
  • pku_Coder
  • pku_Coder
  • 2017-07-04 16:58:14
  • 134

[leetcode] 59.Spiral Matrix II

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order....
  • u014673347
  • u014673347
  • 2015-08-26 23:14:48
  • 454

[leetcode-59]spiral matrixII(java)

问题描述: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral orde...
  • zdavb
  • zdavb
  • 2015-07-10 19:35:45
  • 370
收藏助手
不良信息举报
您举报文章:59. Spiral Matrix II
举报原因:
原因补充:

(最多只允许输入30个字)