Spiral Matrix II 解答
Question
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 ]
]
Solution
Similar with Spiral Matrix.
public class Solution {
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int start = 1, x = 0, y = 0;
int m = n;
while (m > 0) {
if (m == 1) {
result[x][y] = start;
break;
}
for (int i = 0; i < m - 1; i++)
result[x][y++] = start++;
for (int i = 0; i < m - 1; i++)
result[x++][y] = start++;
for (int i = 0; i < m - 1; i++)
result[x][y--] = start++;
for (int i = 0; i < m - 1; i++)
result[x--][y] = start++;
x++;
y++;
m -= 2;
}
return result;
}
}
Spiral Matrix II 解答的更多相关文章
- 【leetcode】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 ...
- 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: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- 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 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- fdm_search_info_w_book_chain
数据知识管理平台-元数据管理-fdm_search_info_w_book_chain-详细信息 fdm_search_info_w_book_chain
- C# - List操作- 去掉重复
ChangeList里面会有重复的数据,这时可以这样去掉重复的item // Remove duplicated info var dup = ChangeList.Where(item => ...
- event.keyCode列表
Keycode对照表 字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 ...
- JAVA并发实现五(生产者和消费者模式Condition方式实现)
package com.subject01; import java.util.PriorityQueue; import java.util.concurrent.locks.Condition; ...
- Annotation(四)——Struts2注解开发
Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...
- ubuntu14.04 + cocos2d-x-2.2.6 + eclipse发布android + Qt Creator4
先把需要的东西准备好,打开控制台,执行以下语句: sudo apt--jdk lib32z1 lib32ncurses5 lib32bz2- 接下来,准备好cocos2d-x-2.2.6和 andro ...
- iPhone 5,6,6 plus 尺寸
- Smack+Openfire 接收和发送文件
转载请注明出处:http://blog.csdn.net/steelychen/article/details/37958839 发送文件须要提供准确的接收放username称(例:user2@192 ...
- Python安装后在CMD命令行下出现“应用程序无法启动.............”问题
问题存在之一:系统是刚刚重做的精简版服务器系统(阉割版) AN就是在阿里云上刚开的Windows Server 2008 系统上碰到的 吓尿了都 症状: 正常安装python环 ...
- JavaScript split()
http://www.w3school.com.cn/jsref/jsref_split.asp