当前位置:网站首页>力扣刷题记录5.1-----59. 螺旋矩阵 II
力扣刷题记录5.1-----59. 螺旋矩阵 II
2022-08-09 01:51:00 【@白圭】
一、题目
二、代码
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
//似乎找到了规律
//减法的规律一次是-1 -1 -2 -2 -3 -3 -4 -4 当减法为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 控制下标的变化,范围都是0-n-1 x代表行 y 代表列
int y=0;
int count_decrease=0;
int maxlength=n-1;
int fill_num=1; //依次填充
int direction=1; //方向号码 1表示右 2表示下 3表示左 4表示上 状态轮转
for(y=0;y<=n-1;y++) //第一行不在规律之中 单独填充 这里y会多加一次 需要减掉
{
return_vector[x][y]=fill_num;
fill_num=fill_num+1;
}
y=y-1; //将y恢复到n-1
//下面的按照规律去填充
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;
}
};
三、运行结果
边栏推荐
猜你喜欢
LeetCode每日两题02:第一个错误的版本 (均1200道)方法:二分查找
New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
【图像去噪】基于边缘增强扩散 (cEED) 和 Coherence Enhancing Diffusion (cCED) 滤波器实现图像去噪附matlab代码
Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached
makefile文件编译
【信号去噪】基于Sage-Husa自适应卡尔曼滤波器实现海浪磁场噪声抑制及海浪磁场噪声的产生附matlab代码
任务五 处理连续型数据
JDBC技术(一)——一个简单的JDBC测试
随机推荐
谷歌翻译软件-免费谷歌翻译
ffplay playback control
《LC刷题总结》—— 二叉树
如何在EasyDSS中使用ffmpeg实现点播视频的拼接与合成?
jetson nano 开机闪一下然后黑屏
右键新建缺少word、excel选项问题处理
LeetCode精选200道--字符串篇
qps tps rps 区别
德语翻译-德语在线批量翻译软件
Latex example reference
HCIP-R&S By Wakin自用笔记(3)OSPF之各类LSA及LSA更新规则
425 Can‘t open data connection for transfer of “/“
ICMP差错报告报文数据字段
睿智的目标检测61——Tensorflow2 Focal loss详解与在YoloV4当中的实现
进程和线程
[机缘参悟-65]:《兵者,诡道也》-6-孙子兵法解读-并战计
JDBC technology (2) - set up common sql and configuration files
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
观察者模式
有相同字符串的查找