Question

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

Solution

Similar with Spiral Matrix.

 public class Solution {
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int start = 1, x = 0, y = 0;
int m = n;
while (m > 0) {
if (m == 1) {
result[x][y] = start;
break;
}
for (int i = 0; i < m - 1; i++)
result[x][y++] = start++;
for (int i = 0; i < m - 1; i++)
result[x++][y] = start++;
for (int i = 0; i < m - 1; i++)
result[x][y--] = start++;
for (int i = 0; i < m - 1; i++)
result[x--][y] = start++;
x++;
y++;
m -= 2;
}
return result;
}
}

Spiral Matrix II 解答的更多相关文章

  1. 【leetcode】Spiral Matrix II

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

  2. 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 ...

  3. 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: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

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

  6. 【leetcode】59.Spiral Matrix II

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

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

  8. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  9. 【leetcode】Spiral Matrix II (middle)

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

随机推荐

  1. poj2823:单调队列入门题

    今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...

  2. 【POJ2136】Vertical Histogram(简单模拟)

    比较简单,按照样例模拟就好!~ #include <iostream> #include <cstdlib> #include <cstdio> #include ...

  3. <转载>C++的链接错误LNK2005

    转载http://bbs.csdn.net/topics/70346371 编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误.弄清楚它形成的原因,就可 ...

  4. NSDate显示和时区的关系

    在网上看到一篇介绍NSDate的博文.在它的“NSDate初始化“章节,说在使用  NSLog(@"当前时间 date = %@",[NSDate date]);时,显示出来的时间 ...

  5. [置顶] Oracle 11g R2 ASM:了解 Oracle ASM 基本概念

    About Oracle ASM Instances About Oracle ASM Disk Groups About Mirroring and Failure Groups About Ora ...

  6. <s:iterator> 序号

    <s:iterator />的序号,解决这个问题有两种办法. 方法一:通过set标签实现: <s:set name="a" value=1/> <s: ...

  7. ZigBee心电传输(一)

    第一次接触模拟的东西哈,也算是一次新的学习旅程以及对ZigBee的再一次探索吧. 首先是方案制定,以及采用芯片AD8232,这样节省了不少时间,把模拟的东西都搬到数字上了,不过还是需要学习不少模电知识 ...

  8. 多路复用I/O select()

    select(),poll(),epoll()的总结:http://www.cnblogs.com/Anker/p/3265058.html 在socket编程中,仅仅使用connect,accept ...

  9. 【转载】CocoaPods安装和使用教程

    转自:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaP ...

  10. js过滤

    datagrid:                loadFilter: function (data) {                    return loadFilter(data);   ...