当前位置:网站首页>【LeetCode】59. 螺旋矩阵 II
【LeetCode】59. 螺旋矩阵 II
2022-06-26 09:36:00 【later_rql】
1.题目描述
给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
2.解题思路
思路:分为四个赋值过程
从左到右 l_r
从上到下 u_d
从右到左 r_l
从下到上 d_u
x记录每一圈的左右,上下边界
y记录每一圈的右左,下上边界
x,y随着圈的缩小,相应变化,x–,y++,
最后考虑n为奇的情况x==y,为arr[x][y]赋值nn
n为偶的不用考虑。**
3.代码
代码一:自己写的,有些冗余。
public static int[][] generateMatrix(int n) {
int[][] arr=new int[n][n];
int l_r,u_d,r_l,d_u,index;
l_r=0;
r_l=n-1;
u_d=0;
d_u=n-1;
index=1;
int x=n-1;//列数
int y=0;//行数
while (index<Math.pow(n,2)) {
//从左到右
while (l_r < x) {
arr[u_d][l_r] = index;
index++;
l_r++;
}
//从上到下
while (u_d < x) {
arr[u_d][l_r] = index;
index++;
u_d++;
}
//从右到左
while (r_l > y) {
arr[u_d][r_l] = index;
index++;
r_l--;
}
//从下到上
while (d_u > y) {
arr[d_u][r_l] = index;
index++;
d_u--;
}
//每执行一圈后,范围缩小
x--;
y++;
l_r = y;
r_l = x;
u_d = y;
d_u = x;
}
if (x==y){
arr[x][y]=n * n;
}
return arr;
}
代码二:笔友写的,非常简洁,易懂。
public static int[][] generateMatrix(int n) {
int l = 0, r = n - 1, t = 0, b = n - 1;
int[][] mat = new int[n][n];
int num = 1, tar = n * n;
while(num <= tar){
for(int i = l; i <= r; i++) mat[t][i] = num++; // left to right.
t++;
for(int i = t; i <= b; i++) mat[i][r] = num++; // top to bottom.
r--;
for(int i = r; i >= l; i--) mat[b][i] = num++; // right to left.
b--;
for(int i = b; i >= t; i--) mat[i][l] = num++; // bottom to top.
l++;
}
return mat;
}
来源:力扣(LeetCode)
链接:link-59.螺旋矩阵 II
边栏推荐
- Today's headline adaptation scheme code
- c语言语法基础之——函数定义学习
- LeetCode 498. 对角线遍历
- Getting started with Flink - word statistics
- Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads
- Download MySQL database installation package website of each system and version
- Solution to network request crash in retrofit2.8.1
- Common SQL add / delete / modify query statements
- VI summary of common commands
- Why do some functions in the go standard library have only signatures but no function bodies?
猜你喜欢
Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads
MapReduce & yarn theory
logback
Redis master-slave replication in win10 system
国际化配置
online trajectory generation
SSM项目小例子,SSM整合图文详细教程
Constraintlayout control uses full Raiders
如何更改微信小程序二维码物料颜色
Mysql database field query case sensitive setting
随机推荐
LeetCode 0710. Random numbers in the blacklist - preprocessing implementation o (1) value
thinkphp6.0的第三方扩展包,支持上传阿里云,七牛云
测试须知——常见接口协议解析
2021-11-12 vrep vision sensor configuration
Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)
WIN10系统实现Redis主从复制
LeetCode 958. 二叉树的完全性校验
Cloud native essay using Hana expression database service on Google kubernetes cluster
LeetCode 基本计算器 224. 227. follow up 394
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.npm ER
My creation anniversary
Learning and understanding of thread pool (with code examples)
Meaning of go runtime
LeetCode 498. 对角线遍历
调用api接口生成不同颜色的微信小程序二维码
The basis of C language grammar -- pointer (multidimensional array, function, summary) learning
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
Common SQL add / delete / modify query statements
Dialog centered
TensorFlow动态分配显存