当前位置:网站首页>Likou 59 - Spiral Matrix II - Boundary Judgment
Likou 59 - Spiral Matrix II - Boundary Judgment
2022-08-03 20:11:00 【Zhang Dui Dui)】
Title description
Given you a positive integer n
, generate a 1
to n2
all elements, and the elements are spirally arranged in clockwise order n x n
Square matrix matrix
.
Solution ideas
- We can create a new n*n empty matrix and add data to it according to the requirements of the title;
- Define the upper, lower, left, and right boundaries of the matrix as t , b , l , r ;
- The new num is initialized to 1 and incremented by 1 after each addition;
- Take a 3*3 matrix as an example: First, you need to fill the first row with 1,2,3 from left to right, so the loop condition at this time should be:
for(int i = l; i <= r; i++) arr[t][i] = num++; t++;
- The purpose of t++ is to move the upper bound (the blue line) down to shrink the matrix;
- Next need to add 4,5, the loop condition is:
for(int i = t; i <= b; i++) arr[i][r] = num++; r--;
- The green right border line moves to the left, and then fills 6,7, the loop condition is:
for(int i = r; i >= l; i--) arr[b][i] = num++; b--;
- The lower border line (red line) moves up, and then fills element 8, the loop condition is:
for(int i = b; i >= t; i--) arr[i][l] = num++; l++;
- The left border line (yellow line) moves to the right, so far the cycle is over, and the outermost layer of elements has been filled;
- If the cycle is not finished, it will continue to repeat the above four steps and continuously adjust the four boundary lines;
- The condition for judging whether the loop is completed is num <= target. If num does not exceed target, it means that the element has not been filled.
Input and output example
Code
class Solution {public int[][] generateMatrix(int n) {int num = 1, target = n*n;int l = 0, r = n-1, t = 0, b = n-1;int[][] arr = new int[n][n];while(num <= target){for(int i = l; i <= r; i++) arr[t][i] = num++;t++;for(int i = t; i <= b; i++) arr[i][r] = num++;r--;for(int i = r; i >= l; i--) arr[b][i] = num++;b--;for(int i = b; i >= t; i--) arr[i][l] = num++;l++;}return arr;}}
边栏推荐
猜你喜欢
149. 直线上最多的点数-并查集做法
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
ECCV 2022 Oral | 满分论文!视频实例分割新SOTA: IDOL
那些年我写过的语言
汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
xss.haozi练习通关详解
友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
机器学习中专业术语的个人理解与总结(纯小白)
随机推荐
glide set gif start stop
RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote
算法--交错字符串(Kotlin)
Solidity智能合约开发 — 4.1-合约创建和函数修饰器
数据驱动的软件智能化开发| ChinaOSC
详解AST抽象语法树
涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
化算力为战力:宁夏中卫的数字化转型启示录
Detailed steps for tensorflow-gpu2.4.1 installation and configuration
Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
揭秘5名运维如何轻松管理数亿级流量系统
Detailed demonstration pytorch framework implementations old photo repair (GPU)
Node version switching tool NVM and npm source manager nrm
ESP8266-Arduino编程实例-MCP4725数模转换器驱动
LeetCode 622. 设计循环队列
ESP8266-Arduino编程实例-WS2812驱动
力扣59-螺旋矩阵 II——边界判断
ThreadLocal详解
Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法