Leetcode: Spiral Matrix II
来源:互联网 发布:jav网络机顶盒v8固件 编辑:程序博客网 时间:2024/06/16 15:21
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
Iterate over n / 2 circles. If n is odd, add n^2 at the center of the matrix.
public class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; int value = 1; for (int i = 0; i < n / 2; i++) { for (int j = i; j < n - 1 - i; j++) { res[i][j] = value++; } for (int j = i; j < n - 1 - i; j++) { res[j][n - 1 - i] = value++; } for (int j = n - 1 - i; j > i; j--) { res[n - 1 - i][j] = value++; } for (int j = n - 1 - i; j > i; j--) { res[j][i] = value++; } } if (n % 2 == 1) { res[n /2][n / 2] = value; } return res; }}
0 0
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix II
- LeetCode : Spiral Matrix II
- [LeetCode] Spiral Matrix II
- [LeetCode]Spiral Matrix II
- [leetcode]Spiral Matrix II
- LeetCode-Spiral Matrix II
- [leetcode] Spiral Matrix II
- LeetCode - Spiral Matrix II
- Leetcode: Spiral Matrix II
- 【LeetCode】Spiral Matrix II
- Leetcode Spiral Matrix II
- [LeetCode] Spiral Matrix II
- LeetCode | Spiral Matrix II
- LeetCode Spiral Matrix II
- Leetcode: Spiral Matrix II
- Spiral Matrix II -- LeetCode
- Android系统添加config.xml 新配置
- js控制合并单元格-行/列
- shell中的四种模式匹配
- oracle
- python的字符串、编码问题
- Leetcode: Spiral Matrix II
- PostgreSQL学习手册6-10
- shell的sort命令的-k参数
- 线程认识
- shell脚本中的sort命令
- 应用系统之间数据传输的几种方式
- 黑马程序员------I/O流
- Java日历的小程序
- Linux Shell之sort命令