当前位置:网站首页>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
边栏推荐
- [detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
- 02. 开发博客项目之数据存储
- 【华为机试真题详解】检查是否存在满足条件的数字组合
- Knowledge points of circular structure
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- [untitled]
- Golang -- TCP implements concurrency (server and client)
- 【torch】|torch.nn.utils.clip_grad_norm_
- Qt TCP 分包粘包的解决方法
- Solution of QT TCP packet sticking
猜你喜欢
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
[cloud native] 3.1 kubernetes platform installation kubespher
剑指 Offer II 039. 直方图最大矩形面积
注释、接续、转义等符号
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
February 12 relativelayout
Unity Vector3. Use and calculation principle of reflect
Implementing fuzzy query with dataframe
Zoom and pan image in Photoshop 2022
【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
随机推荐
UCF(2022暑期团队赛一)
MySQL if and ifnull use
js Array 列表 实战使用总结
浅谈镜头滤镜的类型及作用
關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
C Advanced - data storage (Part 1)
05. 博客项目之安全
[cloud native] 3.1 kubernetes platform installation kubespher
nacos-高可用seata之TC搭建(02)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
HAC cluster modifying administrator user password
Vulhub vulnerability recurrence 71_ Unomi
Vulhub vulnerability recurrence 73_ Webmin
Sliding window problem review
Jvxetable用slot植入j-popup
趋势前沿 | 达摩院语音 AI 最新技术大全
Figure database ongdb release v-1.0.3
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
JS array list actual use summary
04. Project blog log