当前位置:网站首页>Leetcode topic analysis spiral matrix II
Leetcode topic analysis spiral matrix II
2022-06-23 08:39:00 【ruochen】
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
1, 2, 3 ,
8, 9, 4 ,
7, 6, 5
]
public int[][] generateMatrix(int n) {
if (n <= 0) {
return new int[0][0];
}
int[][] matrix = new int[n][n];
int num = 1;
int startx = 0, endx = n - 1;
int starty = 0, endy = n - 1;
while (startx <= endx && starty <= endy) {
// Upper row , From left to right
for (int y = starty; y <= endy; y++) {
matrix[startx][y] = num++;
}
// The column on the right , From top to bottom
for (int x = startx + 1; x <= endx; x++) {
matrix[x][endy] = num++;
}
// If rows or columns are traversed , Then exit the loop
if (startx == endx || starty == endy) {
break;
}
// Row below , From right to left
for (int y = endy - 1; y >= starty; y--) {
matrix[endx][y] = num++;
}
// The column on the left , From bottom to top
for (int x = endx - 1; x >= startx + 1; x--) {
matrix[x][starty] = num++;
}
startx++;
starty++;
endx--;
endy--;
}
return matrix;
}边栏推荐
- How to solve the problem that flv video stream cannot be played and TS file generation fails due to packet loss?
- 鸿蒙读取资源文件
- Analysis of JMeter pressure measurement results
- Monitor the cache update of Eureka client
- Talk about the implementation principle of @autowired
- 3-progressbar and secondary cropping
- On the light application platform finclip and the mobile application development platform mpaas
- 词性家族
- Image segmentation - improved network structure
- Why is the easycvr Video Fusion platform offline when cascading with the Hikvision platform? How to solve it?
猜你喜欢

After reading five books, I summarized these theories of wealth freedom

Captain Abu's soul torture

渲染效果图哪家好?2022最新实测(四)

观察者模式

测试-- 自动化测试selenium(关于API)

The most commonly used 5-stream ETL mode

Deep learning ----- different methods to realize vgg16

What are open source software, free software, copyleft and CC? Can't you tell them clearly?

鸿蒙读取资源文件

坑爹的“敬业福”:支付宝春晚红包技术大爆发
随机推荐
USB peripheral driver - debug
You have a string of code, but do not support the lower version of go; Judge the go version number, you deserve it!
MySQL common skills
usb peripheral 驱动 - configfs
1-gradients, shadows, and text
The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?
Deep learning ----- different methods to implement lenet-5 model
Linux Mysql安装
给你的win10装一个wget
2- use line segments to form graphics and coordinate conversion
What are the PCB characteristics inspection items?
Vulnhub | DC: 4 |【实战】
7-palette-calayer and touch
Vulnhub | DC: 4 | [combat]
Implementing an open source app store with swiftui
[cloud computing] GFS ideological advantages and architecture
Multi Chain and cross chain are the future
Generate code 39 extension code in batch through Excel file
jmeter压测结果分析
How to sort a dictionary by value or key?