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

思路:与上一篇类似。仅仅要不越界就ok

public class Solution {
public int[][] generateMatrix(int n) {
if (n == 0)
return new int[0][0]; int[][] matrix = new int[n][n];
int x1 = 0;
int y1 = 0;
int x2 = matrix.length - 1;
int y2 = matrix[0].length - 1;
int i = 0, j = 1;
while (x1 <= x2 && y1 <= y2) {
// up row
for (i = y1; i <= y2; ++i, j++)
matrix[x1][i] = j;
// right column
for (i = x1 + 1; i <= x2; ++i, j++)
matrix[i][y2] = j;
// bottom row
for (i = y2 - 1; x2 != x1 && i >= y1; --i, j++)
matrix[x2][i] = j;
// left column
for (i = x2 - 1; y1 != y2 && i > x1; --i, j++)
matrix[i][y1] = j; x1++;
y1++;
x2--;
y2--;
}
return matrix;
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

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

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

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. 【leetcode】Spiral Matrix II (middle)

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

  6. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  7. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  8. [leetcode]59. Spiral Matrix II螺旋遍历矩阵2

    Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...

  9. LeetCode题目:Spiral Matrix II

    原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...

随机推荐

  1. Log4Qt使用(三)在DailyRollingFileAppender类中增加属性mMaxBackupIndex

    在Log4Qt中存在一个比较大的问题,当使用 DailyRollingFileAppender对日志进行输出时,会无限输出文件,也就是说,当系统运行很久时,日志文件有可能很大,大到无法想象.因此,很多 ...

  2. Objective-C探究alloc方法的实现

    OS X,iOS中的大部分作为开源软件公开在 Apple Open Source 上.虽然想让大家参考NSObject类的源代码,但是很遗憾,包含NSObject类的Foundation框架没有公开. ...

  3. C#生成Code39(extend)条形码【非条形码字体】

    Code39是条形码的一种.由于编制简单.能够对任意长度的数据进行编码.支持设备广泛等特性而被广泛采用. 能够对任意长度的数据进行编码.其局限在于印刷品的长度和条码阅读器的识别范围. 支持设备广泛.目 ...

  4. [Jquery] 操作html 不常用元素方法大全

    除http://www.w3school.com.cn/jquery/jquery_selectors.asp上的以外该大全应都有. 第一章 input控件篇 1.操作select 下拉框 1.1 获 ...

  5. JS将毫秒转换成时间格式

    JavaScript Date(日期)对象 实例 getTime():返回从 1970 年 1 月 1 日至今的毫秒数. setFullYear(): 设置具体的日期. toUTCString():将 ...

  6. ECstore报表不显示解决

    最近研究ECSTORE发现后台报表显示空白,Google了一下发现N多统一的做法,直接往表里插几条数据.呵呵,更深入一点 1.要显示报表功能首先要确保已经配置好contab的定时任务,定时任务能够执行 ...

  7. Python学习笔记总结(一)对象和流程语句总结

    一.对象类型 1.数字 数字:不可变 2.字符串 字符串:不可原处修改[修改需要创建新的对象],有顺序,支持求长(len),合并(+),重复(*),索引S[0],分片(S[1:3]],成员测试(in) ...

  8. Scala学习笔记--Akka

    待完成 http://www.gtan.com/akka_doc/ http://my.oschina.net/mingdong/blog/297972 http://www.jdon.com/con ...

  9. Jasper_crosstab_group _Error incrementing crosstab dataset

    error detail: net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeExceptio ...

  10. JAVA中this用法小结[转]

    Java关键字this只能用于方法方法体内.当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this.因此,this只能在类中的非静态方法中使用,静 ...