当前位置:网站首页>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
边栏推荐
- NEC infrared remote control coding description
- 使用IDEA开发Spark程序
- Ansible playbook syntax and format automate cloud computing
- 正大国际讲解道琼斯工业指数到底是什么?
- 59、螺旋矩阵(数组)
- The central control learning infrared remote control module supports network and serial port control
- Sim Api User Guide(8)
- 第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
- Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
- SQL调优系列文章之—SQL调优简介
猜你喜欢
随机推荐
Realizing data value through streaming data integration (5) - flow analysis
101. Symmetric Tree
SQL tuning series - Introduction to SQL tuning
454、四数之和(哈希表)
Sim Api User Guide(4)
Sim Api User Guide(8)
Function realization of printing page
lnmp的配置
基于PyQt5实现弹出任务进度条功能示例
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
转:毛姆:阅读是一座随身携带的避难所
实践六 Windows操作系统安全攻防
Can Jerry's AES 256bit [chapter]
通过流式数据集成实现数据价值(1)
NEC infrared remote control coding description
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
DBA常用SQL语句 (5) - Latch 相关
209、长度最小的子数组(数组)
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
IDEA——》每次启动都会Indexing或 scanning files to index








