import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/spiral-matrix-ii/
*
* Created by lverpeng on 2017/7/19.
*
* 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 ]
* ]
*
*/
public class SpiralMatrix2 { public List<Integer[]> build (int n) {
List<Integer[]> result = new ArrayList<Integer[]>();
for (int i = 0; i < n; i++) {
result.add(new Integer[n]);
}
int row = 0;
int col = 0;
int count = 1;
for (; col < (n+1) / 2 && row < (n+1) / 2; row ++, col++) {
for (int i = col; i < n - col; i++) {
result.get(row)[i] = count++;
}
for (int i = row + 1; i < n - row; i ++) {
result.get(i)[n - col - 1] = count++;
}
for (int i = n - col - 2; i >= col; i--) {
result.get(n - row - 1)[i] = count++;
}
for (int i = n - row - 2; i > row; i--) {
result.get(i)[col] = count++;
}
}
return result;
} public static void print (List<Integer[]> list) {
for (Integer[] arr : list) {
System.out.println(Arrays.toString(arr));
}
System.out.println();
} public static void main(String[] args) { SpiralMatrix2 spiralMatrix2 = new SpiralMatrix2();
print(spiralMatrix2.build(0));
print(spiralMatrix2.build(1));
print(spiralMatrix2.build(2));
print(spiralMatrix2.build(3));
print(spiralMatrix2.build(4));
print(spiralMatrix2.build(5));
} }

leetcode — spiral-matrix-ii的更多相关文章

  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] Spiral Matrix II 螺旋矩阵之二

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

  3. [Leetcode] spiral matrix ii 螺旋矩阵

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

  4. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  5. Leetcode Spiral Matrix II

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

  6. LeetCode Spiral Matrix II (技巧)

    题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...

  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

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

  9. LeetCode:Spiral Matrix I II

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

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

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

随机推荐

  1. LOJ-10091(强连通分量)

    题目链接:传送门 思路: 多少头牛收到所有牛头牛的喜欢,喜欢具有传递性,所以将互相喜欢的牛视为一个点,就是有向图的 缩点,收到所有牛的喜欢要求这个“点”没有出度,所以缩点之后统计所有没有出度的点就是结 ...

  2. 非交互式一句话添加root用户

    useradd -p `openssl passwd -1 -salt ‘lsof’ admin123` -u 0 -o -g root -G root -s /bin/bash -d /usr/bi ...

  3. 微信小程序拉起登录的操作

    第一步,前端调用wx.login()接口把token数据请求过来, 第二部,把tok嗯发送到总计的服务器,然后进行微信openid和assession的获取 第三部验证session是否过期,过期重新 ...

  4. 洛谷 p2440木材加工

    #include <iostream>#include <cstring>using namespace std;const int N = 1e5 + 100;int a[N ...

  5. ubuntu+apache2设置访问、重定向到https

    环境:ubunt14裸机,apache2,php5 条件:证书(部分商家买域名送一年),域名,为了方便均在root用户下进行的 web目录:/var/www/test 证书目录(自建):/etc/ap ...

  6. Redhat 6.7 x64升级SSH到OpenSSH_7.4p1完整文档

    原文链接:https://www.cnblogs.com/xshrim/p/6472679.html 导语 Redhat企业级系统的6.7版自带SSH版本为OpenSSH_5.3p1, 基于审计和安全 ...

  7. shell丢弃信息

        1>/dev/null 2>&1 或 >/dev/null 2>&1 的含义及注意事项 > 表示重定向到哪 1表示stdout标准输出,输出到屏幕 ...

  8. html-有趣的标签-会移动的文字

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. Redis-09.慢查询

    慢查询指的是redis命令的执行时间,不包括网络传输和排队时间. Redis配置文件redis.conf中描述慢查询相关的选项在SLOW LOG部分 ######################### ...

  10. Asp.Net WebAPI核心对象解析(三)

    对于.NET的分布式应用开发,可以供我们选择的技术和框架比较多,例如webservice,.net remoting,MSMQ,WCF等等技术.对于这些技术很多人都不会陌生,即时没有深入的了解,但是肯 ...