【LeetCode】59. Spiral Matrix II 解题报告
来源:互联网 发布:mac 网页添加到收藏夹 编辑:程序博客网 时间:2024/06/12 01:50
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51416284
Subject
出处:https://leetcode.com/problems/spiral-matrix/
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.
For example, Given n = 3,
You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
Explain
螺旋遍历往一个n*n的数组中填入数字。方向是→、↓、←、↑,依次循环。
My Solution
使用螺旋遍历输出数组的方法,填入数字即可,数字自增。
参考:http://blog.csdn.net/crazy1235/article/details/51416037
public static int[][] generateMatrix(int n) { int[][] result = new int[n][n]; int top = 0; int right = 0; int bottom = 0; int left = 0; int index = 1; int i = 0; while (true) { // top for (i = left; i < n - right; i++) { result[top][i] = index++; } top++; if (top + bottom == n) { break; } // right for (i = top; i < n - bottom; i++) { result[i][n - 1 - right] = index++; } right++; if (left + right == n) { break; } // bottom for (i = n - 1 - right; i >= left; i--) { result[n - 1 - bottom][i] = index++; } bottom++; if (top + bottom == n) { break; } // left for (i = n - 1 - bottom; i >= top; i--) { result[i][left] = index++; } left++; if (left + right == n) { break; } } return result; }
由于是n * n的数组,所以只需要判断top + bottom 或者left + right即可。将判断条件放到while中。
public static int[][] generateMatrix2(int n) { int[][] result = new int[n][n]; int topOffset = 0; int rightOffset = 0; int bottomOffset = 0; int leftOffset = 0; int index = 1; int i = 0; while (topOffset + bottomOffset < n) { // top for (i = leftOffset; i < n - rightOffset; i++) { result[topOffset][i] = index++; } topOffset++; // right for (i = topOffset; i < n - bottomOffset; i++) { result[i][n - 1 - rightOffset] = index++; } rightOffset++; // bottom for (i = n - 1 - rightOffset; i >= leftOffset; i--) { result[n - 1 - bottomOffset][i] = index++; } bottomOffset++; // left for (i = n - 1 - bottomOffset; i >= topOffset; i--) { result[i][leftOffset] = index++; } leftOffset++; } return result; }
1 0
- [leetcode] 59. Spiral Matrix II 解题报告
- 【LeetCode】59. Spiral Matrix II 解题报告
- [Leetcode] 59. Spiral Matrix II 解题报告
- LeetCode 59. Spiral Matrix II 解题报告
- Leetcode #59. Spiral Matrix ii 螺旋遍历2 解题报告
- [LeetCode]Spiral Matrix,解题报告
- [LeetCode] Spiral Matrix 解题报告
- LeetCode: Spiral Matrix 解题报告
- [leetcode] 54. Spiral Matrix 解题报告
- 【LeetCode】54. Spiral Matrix 解题报告
- [Leetcode] 54. Spiral Matrix 解题报告
- leetcode解题方案--059--Spiral Matrix II
- [LeetCode]59.Spiral Matrix II
- LeetCode --- 59. Spiral Matrix II
- [Leetcode] 59. Spiral Matrix II
- [leetcode] 59.Spiral Matrix II
- LeetCode 59. Spiral Matrix II
- 59. Spiral Matrix II LeetCode
- blender手册快捷键和操作技巧一
- 那些相信天赋的人实际是他们自己能力不高
- 第九周项目训练1
- C语言的未定义行为(undefined behaviour)
- eclipse中Target runtime com.genuitec.runtime.generic.jee60 is not defined.的问题
- 【LeetCode】59. Spiral Matrix II 解题报告
- 进程和cpu的优化
- QT入门,while parsing target library list: not well-formed (invalid token)解决
- 自定义input checkBox的样式
- 超级Cookie可致隐私模式失效 仍可追踪用户信息
- 顺序表的操作
- ArcMap中使用arcpy进行网格截图
- 快速集成android实现listview的字母A-Z排序,界面侧边字母索引
- C++函数指针