当前位置:网站首页>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 can I handle the "unable to load" exception when easyplayer plays webrtcs?
- 2-用线段构成图形、坐标转换
- When easynvr service is started, video cannot be played due to anti-virus software interception. How to deal with it?
- Map interface and its sub implementation classes
- How to migrate x86 architecture applications to arm architecture at low cost
- Vulnhub | DC: 4 |【实战】
- Why do we say that the data service API is the standard configuration of the data midrange?
- Azure Active Directory brute force attack
- Qualcomm 9x07 two startup modes
- 6月《中国数据库行业分析报告》发布!智能风起,列存更生
猜你喜欢

Use of tensorboard

十多年前的入职第一天

数据资产为王,解析企业数字化转型与数据资产管理的关系

The most commonly used 5-stream ETL mode

81 sentences worth repeating

Monitor the cache update of Eureka client

The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?
![[paper notes] catching both gray and black swans: open set supervised analog detection*](/img/52/787b25a9818cfc6a1897af81d41ab2.png)
[paper notes] catching both gray and black swans: open set supervised analog detection*

高通9x07两种启动模式

How to start Jupiter notebook in CONDA virtual environment
随机推荐
Arclayoutview: implementation of an arc layout
How to restore visualizations and dashboards after kibana rebuilds the index
The first day of employment more than ten years ago
Why is the easycvr Video Fusion platform offline when cascading with the Hikvision platform? How to solve it?
1-渐变、阴影和文本
APM performance monitoring practice of jubasha app
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]
单编内核驱动模块
Lighthouse cloud desktop experience
Derivation and loading of the trained random forest model
词性家族
[cloud computing] GFS ideological advantages and architecture
为什么用生长型神经气体网络(GNG)?
Easygbs cannot play video streams in webrtc format. What is the reason?
2- use line segments to form graphics and coordinate conversion
“方脸老师”董宇辉再回应热度下降:把农产品直播做好让农民受益 考虑去支教
2-用线段构成图形、坐标转换
RTSP/ONVIF协议视频平台EasyNVR启动服务报错“service not found”,该如何解决?
Vulnhub | DC: 3 |【实战】
Map interface and its sub implementation classes