当前位置:网站首页>【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
边栏推荐
- Do you know the //go: instructions in the go source code, go:linkname?
- Dialog centered
- cento7.7安装ELK简单记录
- [trajectory planning] testing of ruckig Library
- Getting started with Flink - word statistics
- Redis master-slave replication in win10 system
- Specific implementation comparison between different programming languages
- Get the clicked position in the recyclerview
- 字符串常量池、class常量池和运行时常量池
- 做测试需要知道的内容——url、弱网、接口、自动化、
猜你喜欢

Automated testing -- Introduction and use of pytest itself and third-party modules

Custom interceptor

软件测试---如何选择合适的正交表

What is the web SSH service port of wgcloud

Software testing - how to select the appropriate orthogonal table

Redis notes (13) - scan and keys search for specific prefix key fields (command format, usage examples, locating large keys)

Notes on sports planning on November 22, 2021

定制拦截器

The basis of C language grammar -- pointer (multidimensional array, function, summary) learning

The IE mode tab of Microsoft edge browser is stuck, which has been fixed by rolling back the update
随机推荐
LSP 是什么
Appium自动化测试基础 — 移动端测试环境搭建(二)
exec系列函数(execl、execlp、execle、execv、execvp)使用
LeetCode 0710. Random numbers in the blacklist - preprocessing implementation o (1) value
libmagic 介绍
字符串常量池、class常量池和运行时常量池
cento7.7安装ELK简单记录
Why do some functions in the go standard library have only signatures but no function bodies?
字符串常量池、class常量池和运行时常量池
Code statistics tools cloc and SCC
Glide's most common instructions
c语言语法基础之——函数嵌套、递归 小程序斐波那契之和、阶乘
Vscode common programming fonts
國際化配置
US President signs community safety act to deal with gun issue
MySQL learning summary
DAY 3 数组,前置后置,字符空间,关键词和地址指针
Basic grammar of C language -- pointer (character, one-dimensional array) learning
爬虫相关文章收藏:pyppeteer 、Burpsuite
Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads