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 rowfor (i = y1; i <= y2; ++i, j++)matrix[x1][i] = j;// right columnfor (i = x1 + 1; i <= x2; ++i, j++)matrix[i][y2] = j;// bottom rowfor (i = y2 - 1; x2 != x1 && i >= y1; --i, j++)matrix[x2][i] = j;// left columnfor (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 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  2. LeetCode 59. Spiral Matrix II

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

  3. 59. Spiral Matrix II

    /** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...

  4. LeetCode Spiral Matrix II (生成螺旋矩阵)

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

  5. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

  6. LeetCode 54. Spiral Matrix

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

  7. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  8. LeetCode59 Spiral Matrix II

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

  9. leetcode 54. Spiral Matrix | 54. 螺旋矩阵(Java)

    题目 https://leetcode.com/problems/spiral-matrix/ 题解 类似于状态机的思路,设定一个 padding,走一圈之后,padding+1 .用 长宽 - pa ...

最新文章

  1. 电脑前面的插孔没声音怎么设置_手机没声音是怎么回事
  2. 通过一个图来简单描述一下 socket 链接建立以及通信的模型
  3. python3.6卸载教程_Python3.6安装卸载、执行命令、执行py文件的方法详解
  4. java date 一个月_java中的日期加一个月的计算
  5. java断言错误_java-获取断言错误,即使期望值和实际值相同
  6. 学习计划Current(2019.4.23)
  7. 后台产品基本功:RBAC权限后台角色与权限设计
  8. OpenCV精进之路(二十三):实例——Bag of Features(BoF)图像分类实践
  9. oracle优化查询前几条,一个查询优化的分析调整全过程!很值得一看
  10. ubuntu安装mysql添加密码
  11. 需求:vue+svg实现连线功能
  12. 华为s5720默认用户名和密码_华为交换机s5720s-28p-LI-AC默认用户名和密码是什么?...
  13. WPS如何一页一页设置页眉
  14. 一对一关系,一对多关系,多对多关系的简单理解
  15. Spread控件使用说明_技术部分
  16. 计算机加号公式,怎样在excel表格中显示加号,而不被当成是公式来计算
  17. linux存储实用程序育儿法下载,Linux版utorrent的使用.doc
  18. 「好书推荐」《边城》沈从文的乡土情怀代表作,汪曾祺、林徽因、曹文轩等真情推荐!
  19. 分享10个实用的软件和网站,我每天都在用
  20. react and reduct 学习手记3

热门文章

  1. AJAX全套(JSONP、CORS)
  2. 《转》 在C++中使用TinyXML2解析xml
  3. poj2955 Brackets (区间dp)
  4. perl中-和=作用
  5. MongoDB学习之在Windows下安装MongoDB
  6. Zookeeper的ACL权限控制
  7. 职业教育计算机课教学反思,职高课后的教学反思(精选5篇)
  8. android 通知打开app,Android打开通知栏并回到主页的几种方式
  9. IDEA for win 常用快捷键
  10. 职工考勤管理信息系统数据库课设_职工考勤管理信息系统数据库课程设计