当前位置:网站首页>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;}}边栏推荐
猜你喜欢

华为设备配置VRRP负载分担

Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)

Detailed AST abstract syntax tree

ThreadLocal详解

单调栈及其应用

为什么 BI 软件都搞不定关联分析

高位套牢机构,用友网络的信任危机是如何产生的?

倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
![【微信小程序2】事件传参与数据同步[03]](/img/d9/73004e6edf800c583231a94dfbd878.png)
【微信小程序2】事件传参与数据同步[03]

消除对特权账户的依赖使用Kaniko构建镜像
随机推荐
安装anaconda并创建虚拟环境
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
利用 rpush 和 blpop 实现 Redis 消息队列
leetcode 461. 汉明距离
8.2模拟赛总结
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
嵌入式分享合集27
(十六)51单片机——红外遥控
【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
一种能有效缓解环境噪声对音频质量干扰的方案
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
leetcode 125. 验证回文串
力扣203-移除链表元素——链表
Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
Pytorch GPU 训练环境搭建
Anaconda 虚拟环境迁移
leetcode 326. 3 的幂
钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
leetcode 剑指 Offer 15. 二进制中1的个数