当前位置:网站首页>59. Spiral matrix (array)
59. Spiral matrix (array)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Given a positive integer n, Generate a include 1 To n^2 All the elements , A square matrix in which the elements are spirally arranged in a clockwise order .
Example :
Input : 3 Output : [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
public int[][] generateMatrix(int n){
// Create a 2D array
int [][] res = new int[n][n];
// Define the starting position of each cycle
int startX =0,startY=0;
// How many times per cycle , for example n It's odd 3, that loop=3/2=1, Just a cycle , The in the middle of the matrix only needs to be handled separately
int loop = n/2;
// In the middle of the matrix , for example :n by 3, The middle position is [1,1];
int mid = n/2;
// Used to assign a value to each space in the matrix
int count =1;
// Every cycle , You need to control the length of each edge traversal
int offset =1;
int i,j;
while(loop >0){
i = startX;
j = startY;
// The following four for Is to simulate a turn
// Analog fill uplink from left to right ( Left closed right away )
for (j = startY; j <startY+n-offset ; j++) {
res[startX][j] = count++;
}
// The simulation fills the right column from top to bottom ( Left closed right away )
for (i = startX; i <startX+n-offset ; i++) {
res[i][j] = count++;
}
// The simulation fills down from right to left ( Left closed right away )
for (; j>startY;j--){
res[i][j] = count++;
}
// Simulate filling the left column from bottom to top ( Left closed right away )
for (; i>startX;i--){
res[i][j] = count++;
}
// At the beginning of the second lap , The starting position shall be added with 1,
startX++;
startY++;
//offset Control the length of each edge traversal in each circle
offset+=2;
loop--;
}
// If n It's odd , You need to assign a separate value to the middle of the matrix
if(n%2==1){
res[mid][mid] = count;
}
return res;
}
public static void main(String[] args) {
Luoxuanjuzhen luoxuanjuzhen = new Luoxuanjuzhen();
int [][]res = luoxuanjuzhen.generateMatrix(4);
for (int [] x:res) {
for (int y:x) {
System.out.print(y+" ");
}
}
}
Output results :
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
Time complexity :O(n)
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011141278.html
边栏推荐
- shell脚本免交互
- Realize data value through streaming data integration (1)
- 杰理之通常程序异常情况有哪些?【篇】
- 解决VMware卸载后再安装出现的问题
- DBA常用SQL语句(3)- cache、undo、索引和等待事件
- ansible 云计算 自动化 命令行精简版
- JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
- 利用多线程按顺序连续输出abc10次
- Jerry's users how to handle events in the simplest way [chapter]
- ARM调试(1):两种在keil中实现printf重定向到串口的方法
猜你喜欢
lnmp的配置
MapReduce压缩
Yarn核心参数配置
Question bank and answers of Shanghai safety officer C certificate examination in 2022
101. Symmetric Tree
ARM调试(1):两种在keil中实现printf重定向到串口的方法
Shell script interaction free
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
[untitled]
杰理之更准确地确定异常地址【篇】
随机推荐
203、移出链表元素(链表)
解决方案架构师的小锦囊 - 架构图的 5 种类型
【无标题】
MapReduce核心和基础Demo
Realizing data value through streaming data integration (4) - streaming data pipeline
0704、ansible----01
DBA common SQL statements (3) - cache, undo, index and wait events
《Redis设计与实现》
计算机网络安全实验二|DNS协议漏洞利用实验
SQL调优系列文章之—SQL性能方法论
解决VMware卸载后再安装出现的问题
Sim Api User Guide(4)
CSP certification 202203-2 travel plan (multiple solutions)
Ansible playbook syntax and format automate cloud computing
Sim Api User Guide(6)
242、有效字母异位词(哈希表)
打印页面的功能实现
Detailed explanation of MapReduce calculation process
杰理之系统事件有哪些【篇】
Juc并发编程09——Condition实现源码分析