当前位置:网站首页>Likou Brush Question Record 5.1-----59. Spiral Matrix II
Likou Brush Question Record 5.1-----59. Spiral Matrix II
2022-08-09 02:05:00 【@ Bai Gui】
一、题目
二、代码
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
//Seems to have found a pattern
//The law of subtraction is once-1 -1 -2 -2 -3 -3 -4 -4 When the subtraction is 0 时停止
vector<vector<int> > return_vector(n, vector<int>(n, 0));
//常规变量
int i=0;
int j=0;
int k=0;
//本题变量
int x=0; //x和y Controls the change of subscripts,范围都是0-n-1 x代表行 y 代表列
int y=0;
int count_decrease=0;
int maxlength=n-1;
int fill_num=1; //依次填充
int direction=1; //direction number 1表示右 2表示下 3表示左 4表示上 状态轮转
for(y=0;y<=n-1;y++) //The first line is not in the pattern Fill individually 这里y会多加一次 需要减掉
{
return_vector[x][y]=fill_num;
fill_num=fill_num+1;
}
y=y-1; //将y恢复到n-1
//Fill in the following according to the rules
while(fill_num<=n*n)
{
direction=direction+1; //方向改变
if(direction==5) direction=1;
count_decrease=count_decrease+1;
if(count_decrease==3)
{
count_decrease=1;
maxlength=maxlength-1;
}
if(direction==1) //向右
{
for(i=1;i<=maxlength;i++)
{
// std::cout<<" fill num "<<fill_num<<" "<<" max length "<<maxlength<<std::endl;
y=y+1;
return_vector[x][y]=fill_num;
fill_num=fill_num+1;
}
}
if(direction==2) //向下
{
for(i=1;i<=maxlength;i++)
{
// std::cout<<" fill num "<<fill_num<<" "<<" max length "<<maxlength<<std::endl;
x=x+1;
return_vector[x][y]=fill_num;
fill_num=fill_num+1;
}
}
if(direction==3) //向左
{
for(i=1;i<=maxlength;i++)
{
// std::cout<<" fill num "<<fill_num<<" "<<" max length "<<maxlength<<std::endl;
y=y-1;
return_vector[x][y]=fill_num;
fill_num=fill_num+1;
}
}
if(direction==4) //向上
{
for(i=1;i<=maxlength;i++)
{
// std::cout<<" fill num "<<fill_num<<" "<<" max length "<<maxlength<<std::endl;
x=x-1;
return_vector[x][y]=fill_num;
fill_num=fill_num+1;
}
}
}
return return_vector;
}
};
三、运行结果
边栏推荐
猜你喜欢
class path resource [bean.xml] cannot be opened because it does not 错误解决方案
New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
企业里Foxmail邮箱问题解决方法汇总
数字孪生+燃气管理,开启智慧燃气管理新模式
《LC刷题总结》—— 二叉树
力扣刷题记录2.1-----27. 移除元素
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
使用百度EasyDL实现智能垃圾箱
Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
Etcd realize large-scale application service management of actual combat
随机推荐
如何在群晖系统中安装cpolar(群晖6.X版)
全文翻译:EDPB关于VVA(虚拟语音助理)中处理个人数据的指南02/2021
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
解决有路由策略的情况下域内NAT不通的问题
【HNUMSC】C语言第二讲
typescript91-添加任务基本实现
LeetCode每日两题02:轮转数组 (均1200道)
Go-11 - Process Control
Latex示例参考
力扣刷题记录5.1-----59. 螺旋矩阵 II
Go-11-流程控制
Go - 9 - data type - function
JDBC技术(二)——设置通用的sql和配置文件
Group DETR:分组一对多匹配是加速DETR收敛的关键
torchversion.transforms的使用
年金险的安全性怎么样啊?可靠吗?
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
final
Go-12-Structure
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍