当前位置:网站首页>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;
}
};
三、运行结果
边栏推荐
- Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
- Latex示例参考
- Several ways to use JS to achieve array flattening
- 【HNUMSC】C语言第二讲
- 虹科技术|如何阻止供应链攻击?
- JDBC technology (3) - use Druid database connection pool test
- 力扣刷题记录7.1-----707. 设计链表
- [Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached
- MT4/MQL4入门到精通EA教程第一课-MQL语言常用函数(一)OrderSend()函数
- eladmin容器部署超详细过程
猜你喜欢
Educational Codeforces Round 132 (Rated for Div. 2)
德语翻译-德语在线批量翻译软件
HCIP-R&S By Wakin自用笔记(3)OSPF之各类LSA及LSA更新规则
力扣刷题记录9.1-----24. 两两交换链表中的节点
数字孪生+燃气管理,开启智慧燃气管理新模式
ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...
js实现数组去重的方式(7种)
Codeforces Round #809 (Div. 2)A~D1
解决有路由策略的情况下域内NAT不通的问题
NPDP改版前最后一次考试!请注意
随机推荐
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
2020.12.4 log
Go-7-RESTful API的设计
Likou Brush Question Record 8.1-----206. Reverse linked list
力扣刷题记录10.1-----19. 删除链表的倒数第 N 个结点
Latex示例参考
力扣刷题记录2.1-----27. 移除元素
Mysql 5.7 into the pit
解决有路由策略的情况下域内NAT不通的问题
Difference between KQL and Lucene
德语翻译器在线翻译中文
2022/8/8 比赛思维+状压dp
VOIP使用单端口替换动态端口池进行UDP通信
d初化模板构造器
力扣刷题记录4.1-----209. 长度最小的子数组
MAYA发动机建模
力扣刷题记录3.1-----977. 有序数组的平方
Go-8-Gin framework
项目经理VS产品经理,二者到底有何不同?
torchversion.transforms的使用