spiral matrix II
来源:互联网 发布:海南大学网络教学系统 编辑:程序博客网 时间:2024/06/16 23: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 ]]
public class Solution { public int[][] generateMatrix(int n) { int x = 0; int y = 0; int num = 1; int[][] result = new int[n][n]; while(n > 0) { if(n == 1) { result[y][x] = num++; break; } for(int i = 0; i < n-1; i++) { result[y][x+i] = num++; } for(int i = 0; i < n-1; i++) { result[y+i][x+n-1] = num++; } for(int i = 0; i < n-1; i++) { result[y+n-1][x+n-1-i] = num++; } for(int i = 0; i < n - 1; i++) { result[y+n-1-i][x] = num++; } x++; y++; n = n-2; } return result; }}
0 0
- Spiral Matrix && Spiral Matrix II
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix II
- LeetCode : Spiral Matrix II
- Spiral Matrix II
- [LeetCode] Spiral Matrix II
- Spiral Matrix II
- Spiral Matrix II
- [LeetCode]Spiral Matrix II
- Spiral Matrix II
- Spiral Matrix I(II)
- Spiral Matrix II
- [leetcode]Spiral Matrix II
- LeetCode-Spiral Matrix II
- [leetcode] Spiral Matrix II
- LeetCode - Spiral Matrix II
- Spiral Matrix II
- Codeforces Round #Pi (Div. 2) A B
- 地图(MapKit)
- 我的感想4
- poj-2049 Finding Nemo-BFS
- 闭包阮一峰
- spiral matrix II
- 你所不知道的SQL Server数据库启动过程(用户数据库加载过程的疑难杂症)
- SQLALchemy-查询篇
- A simple problem
- slickedit字体设置——使用 Verdana 字体
- 【计蒜客系列】挑战难题27:三值排序
- JDBC 连接 带实例名的SQL Server
- 【codevs】p1116 四色问题
- Java默认构造函数的作用