题目

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

For example,
Given n = 3,

You should return the following matrix:

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


题解

这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立。
要考虑如果是奇数行列的话,最中心的那个点要单加。 代码如下:
 1     public int[][] generateMatrix(int n) {
 2         int[][] res = new int[n][n];
 3         int k = 1;
 4         int top = 0, bottom = n - 1, left = 0, right = n - 1;
 5         while (left < right && top < bottom) {
 6             for (int j = left; j < right; j++) {
 7                 res[top][j] = k++;
 8             }
 9             for (int i = top; i < bottom; i++) {
                 res[i][right] = k++;
             }
             for (int j = right; j > left; j--) {
                 res[bottom][j] = k++;
             }
             for (int i = bottom; i > top; i--) {
                 res[i][left] = k++;
             }
             left++;
             right--;
             top++;
             bottom--;
         }
         if (n % 2 != 0)
             res[n / 2][n / 2] = k;
         return res;
     }
Reference:leetcodenotes.wordpress.com/2013/11/23/leetcode-spiral-matrix-%E6%8A%8A%E4%B8%80%E4%B8%AA2d-matrix%E7%94%A8%E8%9E%BA%E6%97%8B%E6%96%B9%E5%BC%8F%E6%89%93%E5%8D%B0/

Spiral Matrix II leetcode java的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  3. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  6. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  7. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  8. Java for LeetCode 059 Spiral Matrix II

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

  9. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

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

随机推荐

  1. Bzoj4237 cdq分治+树状数组+单调栈

    二维平面在某区域内点的问题,要么树套树,kdtree,要么就是cdq分治了.然而这题树套树和kdtree都不是很好搞的样子,于是我们就只能cdq分治了.首先把点按照横坐标x排序,在每一层我们需要算出右 ...

  2. "Unchecked-Send"漏洞分析

    author:sf197tl;dr国内并没有一个文档有讲述该漏洞的,正好闲着没事.就写下这篇文章.在网上也搜寻了一些资料,通过自己的翻译才有今天的这篇文章.该漏洞在DASP TOP 10中可以查看到. ...

  3. 堆排序的C++代码实现

    堆排序C++实现 堆排序的具体思路可以查看<算法导论>这本书,一下只提供笔者的C++实现代码,并且将笔者在编写程序的过程当中所遇到的一些细节问题拿出来作一番解释,希望能够对对堆排序有一个透 ...

  4. mobile开发备忘

    css -webkit-tap-highlight-color   webkit点击时会反色,可以清楚 -webkit-appearance   设为none时自带组建样式清除

  5. scrum vs devops vs sre

    DevOps&SRE 超越传统运维之道[北京站] IT大咖说 - 大咖干货,不再错过 http://www.itdks.com/eventlist/detail/908

  6. word标题编号与上一级不一致的解决方法

    前段时间时间就遇到了这个问题,情形如下: 1.标题内容一 1.1二级标题 1.2二级标题 2.第二个一级标题 1.3第二个一级下的二级标题 1.4.就这样乱了 当时搜索一通后很容易就解决了……于是忘记 ...

  7. memcached对key和value的限制 memcached的key最大长度和Value最大长度

    memcached的简单限制就是键(key)和item的限制.最大键长为250个字符.可以接受的储存数据不能超过1MB,因为这是典型slab 的最大值.这里我们可以突破对key长度的限制.问题解决:修 ...

  8. Linux Shell脚本入门--wc命令

    wc 统计文件里面有多少单词,多少行,多少字符. wc语法 [root@www ~]# wc [-lwm] 选项与参数: -l :仅列出行: -w :仅列出多少字(英文单字): -m :多少字符: 默 ...

  9. 基于Linux的智能家居的设计(3)

    2  硬件设计 本课题的硬件设计包含主控制器.传输数据设计.数据採集设计.控制驱动设计.显示设计.门禁设计. 2.1  主控制器 依据方案三选择S3C6410主控芯片,S3C6410是由Samsung ...

  10. 避免string.Format方法的装箱

    我们知道,使用string.Format方法可能会存在装箱的情况.比如如下: static void Main(string[] args) { string s = string.Format(&q ...