当前位置:网站首页>【LeetCode】59. Spiral matrix II
【LeetCode】59. Spiral matrix II
2022-06-26 10:09:00 【later_ rql】
59. Spiral matrix II
1. Title Description
Give you a positive integer n , Generate a include 1 To n2 All the elements , And the elements are arranged in a clockwise spiral order n x n square matrix matrix .
2. Their thinking
Ideas : There are four assignment processes
From left to right l_r
From top to bottom u_d
From right to left r_l
From bottom to top d_u
x Record the left and right of each lap , Up and down the border
y Record the right and left of each lap , Lower and upper boundary
x,y As the circle shrinks , Change accordingly ,x–,y++,
Finally, consider n Strange situation x==y, by arr[x][y] assignment nn
n Don't think about it for me .**
3. Code
Code 1 : His writing , Some redundancy .
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;// Number of columns
int y=0;// Row number
while (index<Math.pow(n,2)) {
// From left to right
while (l_r < x) {
arr[u_d][l_r] = index;
index++;
l_r++;
}
// From top to bottom
while (u_d < x) {
arr[u_d][l_r] = index;
index++;
u_d++;
}
// From right to left
while (r_l > y) {
arr[u_d][r_l] = index;
index++;
r_l--;
}
// From bottom to top
while (d_u > y) {
arr[d_u][r_l] = index;
index++;
d_u--;
}
// After each lap , Narrow the scope
x--;
y++;
l_r = y;
r_l = x;
u_d = y;
d_u = x;
}
if (x==y){
arr[x][y]=n * n;
}
return arr;
}
Code 2 : Written by pen pal , Very concise , Understandability .
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;
}
source : Power button (LeetCode)
link :link-59. Spiral matrix II
边栏推荐
- Tensorflow dynamically allocates video memory
- Does the go compiled executable have dynamic library links?
- LeetCode 958. Completeness checking of binary tree
- [trajectory planning] testing of ruckig Library
- 118. 杨辉三角
- What is the web SSH service port of wgcloud
- c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
- 从tf 1.x到tf 2.6(遇到的就过来更新更新)
- DAY 3 数组,前置后置,字符空间,关键词和地址指针
- 爬虫相关文章收藏:pyppeteer 、Burpsuite
猜你喜欢

Deep learning (tentsorflow2. version) three good student performance problems (1)

logback

Software testing - how to select the appropriate orthogonal table

cmake / set 命令

The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions

Solution to network request crash in retrofit2.8.1

thymeleaf中抽取公共片段

mysql学习总结

首批12家企业入驻!广州首个集中展销老字号产品专柜开张

Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)
随机推荐
When will JVM garbage collection enter the older generation
爬虫相关文章收藏:pyppeteer 、Burpsuite
Constraintlayout control uses full Raiders
Redis novice introduction
Allocation of heap memory when creating objects
Tensorflow dynamically allocates video memory
Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)
Threadmode interpretation of eventbus
thymeleaf中抽取公共片段
Allocation de mémoire tas lors de la création d'objets
【深度优先搜索】312.戳气球
1. 两数之和(LeetCode题目)
Go learning notes (83) - code specification and common development skills
Appium自动化测试基础 — 移动端测试环境搭建(二)
Does the go compiled executable have dynamic library links?
2021-11-12 vrep vision sensor configuration
方法区里面有什么——class文件、class文件常量池、运行时常量池
Redis notes (16) - info instructions and command line tools (view memory, status, number of client connections, monitoring server, scan large keys, sampling server, execute batch commands, etc.)
Specific meaning of go bootstrap
118. 杨辉三角