Spiral Matrix II leetcode java
题目:
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 ]
- ]
题解:
这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立。
要考虑如果是奇数行列的话,最中心的那个点要单加。- 代码如下:
- 1 public int[][] generateMatrix(int n) {
- 2 int[][] res = new int[n][n];
- 3 int k = 1;
- 4 int top = 0, bottom = n - 1, left = 0, right = n - 1;
- 5 while (left < right && top < bottom) {
- 6 for (int j = left; j < right; j++) {
- 7 res[top][j] = k++;
- 8 }
- 9 for (int i = top; i < bottom; i++) {
- res[i][right] = k++;
- }
- for (int j = right; j > left; j--) {
- res[bottom][j] = k++;
- }
- for (int i = bottom; i > top; i--) {
- res[i][left] = k++;
- }
- left++;
- right--;
- top++;
- bottom--;
- }
- if (n % 2 != 0)
- res[n / 2][n / 2] = k;
- return res;
- }
- Reference:leetcodenotes.wordpress.com/2013/11/23/leetcode-spiral-matrix-%E6%8A%8A%E4%B8%80%E4%B8%AA2d-matrix%E7%94%A8%E8%9E%BA%E6%97%8B%E6%96%B9%E5%BC%8F%E6%89%93%E5%8D%B0/
Spiral Matrix II leetcode java的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 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 ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- queue模块回顾
queue queue是python中的标准库,俗称队列. 在python中,多个线程之间的数据是共享的,多个线程进行数据交换的时候,不能够保证数据的安全性和一致性,所以当多个线程需要进行数据交换的时 ...
- Android ListView CheckBox状态错乱(转)
转自:http://www.cnblogs.com/wujd/archive/2012/08/17/2635309.html listView中包含checkBox的时候,经常会发生其中的checkB ...
- Git和Gitlab
参考 http://www.cnblogs.com/clsn/p/7929958.html#auto_id_16https://backlog.com/git-tutorial/cn/intro/in ...
- 从Redis的数据丢失说起(转)
碰到一个悲催的事情:一台Redis服务器,4核,16G内存且没有任何硬件上的问题.持续高压运行了大约3个月,保存了大约14G的数据,设置了比较完备的Save参数.而就是这台主机,在一次重起之后,丢失了 ...
- Oracle数据库备份还原工具之Expdp/IMPdp
使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...
- HDU 4747 Mex (2013杭州网络赛1010题,线段树)
Mex Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- C#打印图片
打印的原理是:生成mdi文件,系统碰到mdi的时候会自动以打印的方式处理.所以,不管用什么模板,什么方式:能在PrintPage事件处理中,生成一张要打印内容的图片就OK了! C#实现打印源码如下: ...
- PHP 如何创建守护(daemon)进程
先讲几个概念 守护进程: Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常常在系统引导装入时启动,在系统关闭时终止. ...
- How To: Implement a Major Upgrade In Your Installer
When creating an .msi-based installer, you are strongly encouraged to include logic that supports Wi ...
- python脚本从excel表到处数据,生成指定格式的文件
#coding:gbk #导入处理excel的模块 import xlrd #定义哪些字段须要推断,仅仅支持时间字段 toSureColArray = ['CREATE_TIME','MODIFY_T ...