59. Spiral Matrix II
来源:互联网 发布:费用优化的步骤 编辑:程序博客网 时间:2024/06/04 00:23
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 ]
]
public class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; int rowbegin = 0, rowend = n-1, colbegin = 0, colend = n-1; int index = 1; while(rowbegin <= rowend && colbegin <= colend){ for(int i = colbegin; i <= colend; i++){ res[rowbegin][i] = index++; } rowbegin++; for(int i = rowbegin; i <= rowend; i++){ res[i][colend] = index++; } colend--; if(rowbegin <= rowend){ for(int i = colend; i >= colbegin; i--){ res[rowend][i] = index++; } } rowend--; if(colbegin <= colend){ for(int i = rowend; i >= rowbegin; i--){ res[i][colbegin] = index++; } } colbegin++; } return res; }}
0 0
- 59.Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- JAVA-AbstractQueuedSynchronizer
- Windows 下安装 MongoDB
- Struts2 HttpServletRequest获取请求参数的过程
- 小图标和文字对齐
- 汇编 bash: ./cpuid: Accessing a corrupted shared library
- 59. Spiral Matrix II
- Codeforces 430C Xor-tree【Dfs+思维】
- mfs master高可用
- 新的一年
- Spring中 PROPAGATION_REQUIRED 解释
- java中队列的使用
- error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
- 1033. 旧键盘打字(20) PAT乙级
- Fiddler 抓包分析