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. struts2参数传递总结

    需求1:登录页面填写表单,提交后进入action,action中能够获取填入的内容.[宏观分类:页面->action] 需求2:登录action从数据库校验完毕后,跳转至主页,主页显示当前登录的 ...

  2. 交叉编译ffmpeg(hi3520d)

    ./configure \--prefix=/usr/local/ffmpeg-3520D \--cross-prefix=/opt/hisi-linux-nptl/arm-hisiv100-linu ...

  3. Equal 路由类

    1.Route 原型 class Route { /* 获取请求路径和查询字符串 */ /* 获取模块.控制器.动作名称 */ /* 获取 URI 参数 */ }

  4. JDBC数据库

    JDBC是Java程序连接和存取数据库的应用程序接口(API),包括两个包:java.sql和javax.sql. 用JDBC访问数据库的一般步骤: 1.建立数据源 2.装入JDBC驱动程序:使用Cl ...

  5. Django积木块八——三级联动

    三级联动 前端需要的效果,省之后市之后现,创建model,查询所有的省的信息,json传到前面,之后通过省的id找到对应的市,是用异步实现的. # model class Sheng(models.M ...

  6. 记录一次Service被注入mapper实例的错误

    在一个搭建框架为SSM的项目中,有一个需求是数据库更新同步Solr索引库的数据. 在使用ActiveMQ作为中间件,实现这个需求时却发生了一个错误. 在Listener实现类里我想注入一个Servic ...

  7. vue solt 属性浅析

    solt 也就是插槽,通俗的讲也是和div,span,p等类似的html标签,只是solt是子组件中的html标签.它显示不显示完全是由父组件决定的, 但是显示的位置是由子组件自己决定的.插槽主要分为 ...

  8. html基础知识梳理

    1.浏览器内核 补充:blink为Google与Opera共同开发. 2.web标准 web标准为w3c和其他标准化组织制定的一系列标准的集合.(标签闭合.小写.不乱嵌套,使用外链css/js;结构行 ...

  9. Android中RecyclerView出现java.lang.IndexOutOfBoundsException

    在RecyclerView更细数据时出现java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder ...

  10. Delphi ClientDataSet复制记录

    数据源记录集:ClientDataSetSource:目标记录集:ClientDataSetCopy 1)复制一条记录. ClientDataSetCopy.Close;  ClientDataSet ...