当前位置:网站首页>Spiral matrix
Spiral matrix
2022-07-28 08:03:00 【The galloping snail has been occupied】
Title Description :
Given a positive integer n, Generate a include 1 To n2 All the elements , A square matrix in which the elements are spirally arranged in a clockwise order .
Ideas :
Generate a n * n The empty matrix of , And then in order , Then simulate the whole inward surrounding filling process :
- Define the current left and right upper and lower boundaries l,r,t,b, Initial value num = 1, Iteration termination value tar = n * n;
- When num <= tar when , Always follow From left to right From top to bottom From right to left From bottom to top Fill in the sequence loop , After each filling :
- perform num += 1: Get the next number to fill in ;
- Update boundaries : For example, after filling in from left to right , Upper boundary t += 1, Equivalent to the upper boundary shrinking inward 1.

Code :
public static void solution(int n){
int[][] a = new int[n][n];
//l,r,t,b Define the boundary of the matrix
/** * l ->left * r ->right * t ->top * b ->buttom */
int l=0,r =n-1,t=0,b=n-1;
int number =1;
int tar = n*n;
while(number <= tar){
/** * left -> right * */
for(int i=l;i<=r;i++){
a[t][i]=number;
number++;
}
t++;
/** * top -> buttom * */
for (int i = t; i <=b ; i++) {
a[i][r] =number ;
number ++;
}
r--;
/** * * right -> left */
for (int i = r; i >= l ; i--) {
a[b][i]=number;
number++;
}
b--;
/** * * buttom -> top */
for (int i = b; i >=t; i--) {
a[i][l]=number;
number++;
}
l++;
}
}
In this paper, the reference leetcode
边栏推荐
- DNA-Ag2SQDs脱氧核糖核酸DNA修饰硫化银Ag2S量子点的合成方法
- ArcGIS JS map internal and external network environment judgment
- 使用FFmpeg来批量生成单图+单音频的一图流视频
- Niuke MySQL - SQL must know and know
- Opentsdb time series database
- Awk from introduction to earth (16) discussion on the types of awk variables -- about the two types of numbers and strings
- 03 | project deployment: how to quickly deploy a website developed based on the laravel framework
- In QT multithreading, in which thread does the slot function perform analysis
- Huawei Senior Engineer -- BGP routing filtering and community attributes
- ArcGIS JS customizes the accessor and uses the watchutils related method to view the attribute
猜你喜欢

Oracle local network service

Synthesis of dna-ag2sqds DNA modified silver sulfide Ag2S quantum dots

DNA cuinseqds near infrared CuInSe quantum dots wrapped deoxyribonucleic acid DNA

EMC design strategy - clock

Disassemble Huawei switches and learn Basic EMC operations

Parse tree structure JS

EMC设计攻略 —时钟

It has been rectified seven times and took half a month. Painful EMC summary

CLion调试redis6源码

解决CNN固有缺陷!通用 CNN 架构CCNN来了| ICML2022
随机推荐
It has been rectified seven times and took half a month. Painful EMC summary
华为高级工程师---BGP路由过滤及社团属性
聊一聊数据库的行存与列存
2022年湖南工学院ACM集训第五次周测AD题题解
Daily question - split equal sum subset
Introduction to magnetic ring selection and EMC rectification skills
XSS知识点和20字符短域名绕过
Flowable workflow all business concepts
Record a MYCAT connection and solve the problems of communications link failure
How to analyze the taxi business problem of didi SQL interview question
Mysql, how many columns can be used to create an index?
DNA修饰贵金属纳米颗粒|DNA修饰纳米铜颗粒CuNPS-DNA|研究要点
leetcode 字符串类
【活动报名】云原生技术交流 Meetup,8 月 6 日广州见
基于单例模式的yaml参数配置
Using identity framework to realize JWT identity authentication and authorization in.Net 6.0
[Google] solve the problem that Google browser does not pop up the account and password save box and cannot save login information
node(一)
How to understand the adjective prefix of socket: "connection oriented" and "connectionless"
After being accidentally dragged into QQ fraud group