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. java算法之身份证号码验证

    调用时直接 new IDCard().verify(身份证id);就可以了 实现代码如下: public class IDCard { private String _codeError; //wi ...

  2. 第26讲 对话框AlertDialog的自定义实现

    第26讲对话框AlertDialog的自定义实现 比如我们在开发过长当中,要通过介绍系统发送的一个广播弹出一个dialog.但是dialog必需是基于activity才能呈现出来,如果没有activi ...

  3. Webservice-Java-CXF

    1)首先呢,还是包的问题,在http://cxf.apache.org/download.html这里可以下到最新版的CXF,当然,我用的是最新版的.接下来还是那句废话,建WEB项目,放入JAR包.而 ...

  4. 高性能MySql进化论【转】

    高性能MySql进化论(十二):Mysql中分区表的使用总结 http://binary.duapp.com/category/sql 当数据量非常大时(表的容量到达GB或者是TB),如果仍然采用索引 ...

  5. poj 2041 Unreliable Message 字符串处理

    水的问题.直接附着到代码. //poj 2041 //sep9 #include <iostream> using namespace std; char mode[128]; char ...

  6. HDOJ 4937 Lucky Number

    当进制转换后所剩下的为数较少时(2位.3位),相应的base都比較大.能够用数学的方法计算出来. 预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算. . .. Lucky Numb ...

  7. Android中Service类onStartCommand

    Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...

  8. python - 类的字段

    一.静态字段:保存在类里面 1.创建静态字段: class Foo: CC = 123 # 字段(静态字段),保存在类里 def __init__(self): self.name = 'alex' ...

  9. 程序员的家!我终于拥有自己的blog了!!!

    经过多次提交诚恳的家园申请,终于得到了审核通过!今天就开始了我的.net成长之路!!!

  10. android-用xml自定义背景(可自定义显示具体那一边)

    常见的描边都是闭合的.四个边都有.如下: <?xml version="1.0" encoding="UTF-8"?> <layer-list ...