当前位置:网站首页>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
边栏推荐
- Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
- [mask requirements of OSPF and Isis in multi access network]
- 【云原生】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
- 剑指 Offer II 039. 直方图最大矩形面积
- Fluent implements a loadingbutton with loading animation
- 01. 开发博客项目之项目介绍
- Codeforces Round #804 (Div. 2) Editorial(A-B)
- February 12 relativelayout
- 【华为机试真题详解】检查是否存在满足条件的数字组合
猜你喜欢
Vite configures the development environment and production environment
Self built DNS server, the client opens the web page slowly, the solution
Review of double pointer problems
February 12 relativelayout
指針經典筆試題
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
ARTS Week 25
Codeforces Round #804 (Div. 2) Editorial(A-B)
Nacos - TC Construction of High available seata (02)
Zoom and pan image in Photoshop 2022
随机推荐
Select knowledge points of structure
Deep learning -yolov5 introduction to actual combat click data set training
Please wait while Jenkins is getting ready to work
用StopWatch 统计代码耗时
Nacos - TC Construction of High available seata (02)
Vulhub vulnerability recurrence 69_ Tiki Wiki
Some common skills on unity inspector are generally used for editor extension or others
Knowledge points of circular structure
算法-- 爬楼梯(Kotlin)
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
Idea one key guide package
F12 solve the problem that web pages cannot be copied
[effective Objective-C] - memory management
[leetcode16] the sum of the nearest three numbers (double pointer)
In 2022, we must enter the big factory as soon as possible
Sword finger offer II 039 Maximum rectangular area of histogram
Easy to understand I2C protocol
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Excel转换为Lua的配置文件