当前位置:网站首页>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
边栏推荐
猜你喜欢
0704、ansible----01
Zhengda international explains what the Dow Jones industrial index is?
Failureforwardurl and failureurl
101. Symmetric Tree
2022 mobile crane driver test question bank simulation test platform operation
第120章 SQL函数 ROUND
Jerry's more accurate determination of abnormal address [chapter]
MapReduce计算流程详解
【无标题】
Sim Api User Guide(5)
随机推荐
454、四数之和(哈希表)
lnmp的配置
art-template 模板引擎
Realizing data value through streaming data integration (5) - stream processing
SQL tuning series - Introduction to SQL tuning
Juc并发编程06——深入剖析队列同步器AQS源码
Realizing data value through streaming data integration (5) - flow analysis
Jerry's users how to handle events in the simplest way [chapter]
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
Custom login failure handling
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
What are Jerry's usual program exceptions? [chapter]
MapReduce计算流程详解
MapReduce压缩
Odoo server setup notes
Question bank and answers of Shanghai safety officer C certificate examination in 2022
0704、ansible----01
Common DBA SQL statements (4) - Top SQL
Rain produces hundreds of valleys, and all things grow
CSP certification 202203-2 travel plan (multiple solutions)