当前位置:网站首页>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
边栏推荐
- nacos-高可用seata之TC搭建(02)
- Fluent implements a loadingbutton with loading animation
- Vite configures the development environment and production environment
- 01. 开发博客项目之项目介绍
- Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
- Knowledge points of circular structure
- 毕业设计游戏商城
- Driver development - hellowdm driver
- Zoom and pan image in Photoshop 2022
- pix2pix:使用条件对抗网络的图像到图像转换
猜你喜欢
Fiddler installed the certificate, or prompted that the certificate is invalid
Questions d'examen écrit classiques du pointeur
Imperial cms7.5 imitation "D9 download station" software application download website source code
In 2022, we must enter the big factory as soon as possible
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
Promise summary
Excel转换为Lua的配置文件
First acquaintance with CDN
C进阶-数据的存储(上)
Codeforces Round #804 (Div. 2) Editorial(A-B)
随机推荐
nacos-高可用seata之TC搭建(02)
nacos-高可用seata之TC搭建(02)
Pickle and savez_ Compressed compressed volume comparison
Nacos - TC Construction of High available seata (02)
Oracle query table index, unique constraint, field
01. 开发博客项目之项目介绍
Knowledge points of circular structure
Check the useful photo lossless magnification software on Apple computer
Vulhub vulnerability recurrence 71_ Unomi
Note the various data set acquisition methods of jvxetable
用StopWatch 统计代码耗时
Three methods of Oracle two table Association update
2022 half year summary
指针经典笔试题
Golang -- TCP implements concurrency (server and client)
Sorting out the knowledge points of multicast and broadcasting
Microblogging hot search stock selection strategy
Detailed summary of SQL injection
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)