当前位置:网站首页>leetcode 59. Spiral matrix II
leetcode 59. Spiral matrix II
2022-04-21 07:46:00 【Konjaku in Grade 5 of primary school】
leetcode 59. Spiral matrix Ⅱ
Hello everyone , I'm studying konjak in grade five of primary school , Focus on the back end , Witness the growth of konjaku together , Your comments, praise and attention are my biggest motivation , If you have any mistakes, please let me know , Thank you very much . Let's support originality ! There is a mistake in writing .
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 .

Input n=3
Output [[1,2,3],[8,9,4],[7,6,5]]
Answer key
The main thing is to find out the conditions for each turn , Then keep turning ( In whole circles )
/* * @lc app=leetcode.cn id=59 lang=cpp * * [59] Spiral matrix II */
// @lc code=start
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> res(n, vector<int>(n, 0)); // Define an array for storing data
int startx = 0, starty = 0; // Definition x and y Axis
int loop = n / 2; // Calculate the number of times the created matrix needs to be cycled
int mid = n / 2; // Calculate the element subscript of the intermediate element
int count = 1; // Count assignment
int offset = 1; // Control the length of the edge traversal field of each layer
int i, j;
while (loop--)
{
// The data of the above row is filled in
for (j = starty; j < starty + n - offset; j++)
{
res[startx][j] = count++;
/* code */
}
// Filling of data on the right
for (i = startx; i < startx + n - offset; i++)
{
res[i][j] = count++;
}
// Fill in the following data
for (; j > starty; j--)
{
res[i][j] = count++;
/* code */
}
// Filling of data on the left
for (; i > startx; i--)
{
res[i][j] = count++;
/* code */
}
// Shrink the subscript in one circle for each circle filled
starty++;
startx++;
// Control the traversal times of each edge, and cannot reach the last block
offset += 2;
/* code */
}
if (n % 2)
{
res[mid][mid] = count;
}
return res;
}
};
// @lc code=end
count;
}
return res;
}
};
// @lc code=end
版权声明
本文为[Konjaku in Grade 5 of primary school]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626569355.html
边栏推荐
猜你喜欢

.net core 部署至Linux平台上访问SQL SERVER 2008 R2时提示连接超时异常

playwright,selenium,操作ifram元素

Enterprise service bus -- Introduction to muleesb

企业服务总线--MuleESB简介

2020-12-21至2022-1-2:解析观察者模式

论文阅读:Domain Name Encryption Is Not Enough: Privacy Leakage via IP-based Website Fingerprinting
![Navicat连Oracle报错[No matching authentication protocol]](/img/16/4dd115fc5abc68f7d1ffa640165fd9.png)
Navicat连Oracle报错[No matching authentication protocol]

Principle and introduction of elk log analysis system

sqlserver 两个表之间进行模糊查询,sqlserver 导入excel数据步骤
![Navicat even Oracle reports an error [no matching authentication protocol]](/img/16/4dd115fc5abc68f7d1ffa640165fd9.png)
Navicat even Oracle reports an error [no matching authentication protocol]
随机推荐
ETL之Kettle工具十大功能特性详解
Leetcode 1387.将整数按权重排序(Sort Integers by The Power Value)
VS2019官方的免费打印控件
Principle and introduction of elk log analysis system
.net core 部署至Linux平台上访问SQL SERVER 2008 R2时提示连接超时异常
Text Templates
C# WebService 接口 通过Request请求获取json参数
根据二维数组某个字段的值查找数组
Exit and Status
JSON编码解码
论文阅读:Social Media and Fake News in the 2016 Election
sqlmap post 注入方法
Common weak passwords in network security devices
php 判断是不是同一个月
设置谷歌浏览器深色黑色背景
论文阅读:Measuring the Global Recursive DNS Infrastructure: A View From the Edge
2022-1-3至2022-1-16 访问者模式
Ctf-rsa decryption script
SQLServer的3种分页查询方式sql2000/2008/2012的
PHP 对象转数组