Leetcode 59. Spiral Matrix II
2016-12-31 11:46
375 查看
public class Solution { public int[][] generateMatrix(int n) { int[][] res = new int ; int up = 0, right = n-1, down = n-1, left = 0, elem = 1; while (true) { for (int col=left; col<=right; col++) res[up][col] = elem++; if (++up > down) break; for (int row=up; row<=down; row++) res[row][right] = elem++; if (--right < left) break; for (int col=right; col>=left; col--) res[down][col] = elem++; if (--down < up) break; for (int row=down; row>=up; row--) res[row][left] = elem++; if (++left > right) break; } return res; } }
相关文章推荐
- Leetcode 59. Spiral Matrix II
- LeetCode 59. Spiral Matrix II
- LeetCode59——Spiral Matrix II
- [LeetCode 59] Spiral Matrix II
- leetcode 59: Spiral Matrix II
- [LeetCode]59 Spiral Matrix II
- LeetCode 59 Spiral Matrix II
- leetcode59 Spiral Matrix II
- [leetcode] 59.Spiral Matrix II
- LeetCode------54. Spiral Matrix(螺旋二维矩阵)-----59. Spiral Matrix II(n的螺旋矩阵)
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
- <LeetCode OJ> 54 / 59 Spiral Matrix( I / II )
- 【leetcode】【59】Spiral Matrix II
- 【LeetCode】C# 59、Spiral Matrix II
- leetcode 59. Spiral Matrix II
- LeetCode59 Spiral Matrix II
- 【LeetCode】(59)Spiral Matrix II(Medium)
- leetcode 59. Spiral Matrix II
- 【leetcode】59. Spiral Matrix II
- LeetCode 54/59. Spiral Matrix i, ii