[leetCode] Spiral Matrix II
来源:互联网 发布:js获取input file路径 编辑:程序博客网 时间:2024/06/05 06:18
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[][] res = new int[n][n]; int x0 = 0, x1 = n - 1, y0 = 0, y1 = n - 1; int ele = 0; while (x0 <= x1 && y0 <= y1) { for (int i = y0; i <= y1; i++) { res[x0][i] = ++ele; } for (int i = x0 +1; i <= x1; i++) { res[i][y1] = ++ele; } if (x0 == x1) break; for (int i = y1 - 1; i >= y0; i--) { res[x1][i] = ++ele; } if (y0 == y1) break; for (int i = x1 - 1; i > x0; i--) { res[i][y0] = ++ele; } x0++; x1--; y0++; y1--; } 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内存溢出
- PHP学习之路第八页
- C语言: 结构体的赋值
- getchar scanf 缓冲区 清除回车符
- egl gles2.0开发
- [leetCode] Spiral Matrix II
- 《中华人民共和国著作权法》第二十二条
- const和#define宏定义的区别
- 生成系列数(1、2、3 ... N)应用实例
- C++类的前置声明
- std::function和std::bind的使用(总结)
- Remove Nth Node From End of List
- ViewGroup重写——滚动页面容器
- linux shell中的点命令------谈谈. ./a.sh和./a.sh的区别