【Leetcode】Spiral Matrix II (Rotated)
来源:互联网 发布:特殊字体生成器软件 编辑:程序博客网 时间:2024/06/06 18:43
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
这道题让螺旋存入数据,和I几乎差不多,唯一的不同在于不存在只有一行或一列的情况,因为是n-square的,所以只有n为奇数的时候会多出中间的元素,所以可以先把中间的元素先解决了,然后再处理其他的,思路和I差不多,如有不懂可以去参考I
public int[][] generateMatrix(int n) {int[][] result = new int[n][n];int row = n;int col = n;int x = 0;int y = 0;int num = 1;if (n % 2 != 0)result[n / 2][n / 2] = n * n;while (row > 0 && col > 0) {for (int i = 0; i < col - 1; i++)result[x][y++] = num++;for (int i = 0; i < row - 1; i++)result[x++][y] = num++;for (int i = 0; i < col - 1; i++)result[x][y--] = num++;for (int i = 0; i < row - 1; i++)result[x--][y] = num++;row = row - 2;col = col - 2;x++;y++;}return result;}
0 0
- 【Leetcode】Spiral Matrix II (Rotated)
- 【Leetcode】Spiral Matrix (Rotated)
- 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
- tomcat服务启动又停止,因 1 (0x1) 服务性错误而停止等问题
- Tomcat设置多站点
- 金千鎰과 論介
- 【Leetcode】Spiral Matrix (Rotated)
- OFBIZ视频教程
- 【Leetcode】Spiral Matrix II (Rotated)
- 《炉石传说》架构设计赏析(6):卡牌&技能数据的运行时组织
- glMatrixMode
- 计算并集
- LeetCode: Maximum Product Subarray
- 内部类——局部内部类
- hdu 5082 Love(Bestcoder Round #15)
- hdu 5083 Instruction(Bestcoder Round #15)
- 【Leetcode】Rotate Image (Rotated)