Spiral Matrix II
来源:互联网 发布:阿里云短信授权委托书 编辑:程序博客网 时间:2024/05/23 09:58
Spiral Matrix II
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 ]]Java代码:
public class Solution { public int[][] generateMatrix(int n) { int[][] ans = new int[n][n]; int curr = 1; for (int i = 0, j = 0; i<(n+1)/2 ; ++i, ++j){ for (int k = j; k<n-j; ++k){ ans[i][k] = curr++; } if (i>=n/2) break; // this is needed for odd n's. for (int k = i+1; k<n-1-i; ++k){ ans[k][n-1-j]=curr++; } for (int k = n-1-j; k>j; --k){ ans[n-1-i][k] = curr++; } for (int k = n-1-i; k>i; --k){ ans[k][j] = curr++; } } return ans;}}
0 0
- Spiral Matrix && Spiral Matrix II
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix II
- LeetCode : Spiral Matrix II
- Spiral Matrix II
- [LeetCode] Spiral Matrix II
- Spiral Matrix II
- Spiral Matrix II
- [LeetCode]Spiral Matrix II
- Spiral Matrix II
- Spiral Matrix I(II)
- Spiral Matrix II
- [leetcode]Spiral Matrix II
- LeetCode-Spiral Matrix II
- [leetcode] Spiral Matrix II
- LeetCode - Spiral Matrix II
- Spiral Matrix II
- 控件内容错位
- PHP导入Excel到数据库
- 虚拟机网络设置
- Lambda表达式和Linq实现数据集的简单筛选并排序
- JAVA笔记:Java 类集总结(二、Map接口及相关)
- Spiral Matrix II
- Adb connection Error:远程主机强迫关闭了一个现有的连接
- 理解 Java 序列化
- 在页面上屏蔽文字图片的选中功能
- 输入框js限制
- CString转char * ,string
- Single-Record Statistics
- HDU-bee-蜜蜂行走路线
- 黑马程序员_JAVA语言基础组成_判断结构