当前位置:网站首页>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
边栏推荐
- Summary of deep learning tuning tricks
- Some common skills on unity inspector are generally used for editor extension or others
- Unity Vector3. Use and calculation principle of reflect
- Deep learning -yolov5 introduction to actual combat click data set training
- HAC集群修改管理员用户密码
- 【LeetCode】18、四数之和
- Vulhub vulnerability recurrence 68_ ThinkPHP
- MySQL time processing
- C进阶-数据的存储(上)
- TCP three handshakes you need to know
猜你喜欢
Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
Figure database ongdb release v-1.0.3
Summary of redis basic knowledge points
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
【云原生】3.1 Kubernetes平台安装KubeSpher
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
Implementing fuzzy query with dataframe
注释、接续、转义等符号
Vulhub vulnerability recurrence 73_ Webmin
pix2pix:使用条件对抗网络的图像到图像转换
随机推荐
04. 项目博客之日志
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
Collection + interview questions
Mongodb basic knowledge summary
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
TCP three handshakes you need to know
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
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
How to get list length
UCF (summer team competition II)
[effective Objective-C] - memory management
Fiddler installed the certificate, or prompted that the certificate is invalid
[leetcode] 18. Sum of four numbers
Talking about the type and function of lens filter
C# AES对字符串进行加密
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
毕业设计游戏商城
【torch】|torch. nn. utils. clip_ grad_ norm_
Configuration file converted from Excel to Lua