当前位置:网站首页>59. Spiral matrix
59. Spiral matrix
2022-07-06 05:27:00 【365JHWZGo】
subject
''' Description: 59. Spiral matrix Autor: 365JHWZGo Date: 2022-02-12 20:50:58 LastEditors: 365JHWZGo LastEditTime: 2022-02-12 21:06:35 '''
Ideas
I think it's a spiral to get the answer , There is no real solution , Just solve it directly by violence , That is, the results are obtained in order .
When n=10 Schematic answer when
[[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[36, 37, 38, 39, 40, 41, 42, 43, 44, 11],
[35, 64, 65, 66, 67, 68, 69, 70, 45, 12],
[34, 63, 84, 85, 86, 87, 88, 71, 46, 13],
[33, 62, 83, 96, 97, 98, 89, 72, 47, 14],
[32, 61, 82, 95, 100, 99, 90, 73, 48, 15],
[31, 60, 81, 94, 93, 92, 91, 74, 49, 16],
[30, 59, 80, 79, 78, 77, 76, 75, 50, 17],
[29, 58, 57, 56, 55, 54, 53, 52, 51, 18],
[28, 27, 26, 25, 24, 23, 22, 21, 20, 19]]
Code
class Solution(object):
def generateMatrix(self, n):
""" :type n: int :rtype: List[List[int]] """
if n == 1:
return [[1]]
if n == 2:
return [[1,2],[4,3]]
demo = [[i+1 for i in range(n)]for _ in range(n)]
# print(demo)
final = n**2 # The target
num = n+1 # The number
i,j = 0,n-1 # Subscript
time = 1
U,D,L,R = n-time-1,n-time,n-time-1,n-time
while num<=final:
if R:
i+=1
demo[i][j] = num
R-=1
num+=1
elif D:
j-=1
demo[i][j] = num
D-=1
num+=1
elif L:
i-=1
demo[i][j] = num
L-=1
num+=1
elif U:
j+=1
demo[i][j] = num
U-=1
num+=1
else:
time+=2
U,D,L,R = n-time-1,n-time,n-time-1,n-time
return demo

边栏推荐
- 无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
- Vulhub vulnerability recurrence 69_ Tiki Wiki
- Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
- Cuda11.1 online installation
- [leetcode16] the sum of the nearest three numbers (double pointer)
- Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
- [detailed explanation of Huawei machine test] statistics of shooting competition results
- Force buckle 1189 Maximum number of "balloons"
- Safe mode on Windows
- Jvxetable implant j-popup with slot
猜你喜欢

Check the useful photo lossless magnification software on Apple computer

Notes, continuation, escape and other symbols

Sword finger offer II 039 Maximum rectangular area of histogram

Simple understanding of interpreters and compilers

趋势前沿 | 达摩院语音 AI 最新技术大全

Modbus协议通信异常

02. Develop data storage of blog project

毕业设计游戏商城

02. 开发博客项目之数据存储

03. Login of development blog project
随机推荐
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
Nacos - TC Construction of High available seata (02)
Steady, 35K, byte business data analysis post
Excel转换为Lua的配置文件
Vulhub vulnerability recurrence 73_ Webmin
Idea one key guide package
Yyds dry inventory SSH Remote Connection introduction
JS array list actual use summary
Safe mode on Windows
Detailed summary of SQL injection
關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
用StopWatch 统计代码耗时
【torch】|torch.nn.utils.clip_grad_norm_
Codeforces Round #804 (Div. 2) Editorial(A-B)
2022半年总结
Three. JS learning - light and shadow (understanding)
Algorithm -- climbing stairs (kotlin)
Mysql高级篇学习总结9:创建索引、删除索引、降序索引、隐藏索引
Cuda11.1 online installation
[detailed explanation of Huawei machine test] statistics of shooting competition results