当前位置:网站首页>Daily question - leecode59 (spiral matrix II)
Daily question - leecode59 (spiral matrix II)
2022-06-09 20:34:00 【InfoQ】
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
# Initialize the square to fill
matrix = [[0] * n for _ in range(n)]
left, right, up, down = 0, n - 1, 0, n - 1
number = 1 # Number to fill
while left < right and up < down:
# Fill the upper edge from left to right
for x in range(left, right):
matrix[up][x] = number
number += 1
# Fill the right side from top to bottom
for y in range(up, down):
matrix[y][right] = number
number += 1
# Fill the bottom from right to left
for x in range(right, left, -1):
matrix[down][x] = number
number += 1
# Fill the left side from bottom to top
for y in range(down, up, -1):
matrix[y][left] = number
number += 1
# Narrow the range to fill
left += 1
right -= 1
up += 1
down -= 1
# If the order is odd , Fill the center once more
if n % 2:
matrix[round(n // 2)][round(n // 2)] = number
#round() Method returns a floating-point number x Round the value of .
return matrix边栏推荐
- Automatically update domain name resolution to local IP, DDNS
- C # learning about abstract classes
- Xcode 14 brings new improvements, and Xcode cloud is officially launched!
- VFP在64位win10环境下访问oracle出现的问题及解决方案
- C#接口类的学习
- Just learning embedded, I want to ask what is interrupt and what is the concept of interrupt
- ModelArts存储方案建议
- Potential functions commonly used in lammps and collection of crystal library resources
- Some applications between delegates and events in C #
- Edge browser setting network speed limit
猜你喜欢

VFP在64位win10环境下访问oracle出现的问题及解决方案

Changshu science and technology applet SQL injection

C language to realize computer automatic shutdown program -- it can be used to spoof roommate's computer

Edge browser setting network speed limit

Ceisum三维场景demo

Unity UI scrollbar component

neo4j图数据库导出csv文件

Neo4j桌面版数据库备份

HMI 串口屏 SD卡/TF卡升级的 bug
![[tgowt] cmake to Ninja construction](/img/e9/8ce56c421ee98c0b36464fc65dd39d.png)
[tgowt] cmake to Ninja construction
随机推荐
A potential bug in creating project generated word library in HMI
Inheritance relationship in C #
SSL (secure socket layer) digital certificate
Skills and experience of product planning
Niuke network: some sorting problems about the k-th number
Query SQL
Lammps中常用的势函数和晶体库资源收集
C#中的继承关系
Neo4j桌面版数据库备份
杰理之有关摄像头帧数,以及图层【篇】
Apple Announces Winner of the 2022 Apple Design Award
JVM-字节码是如何被JVM执行的+关于线程一点思想引子
Jerry's modification to improve the output frame rate of camera source video [chapter]
平安证券开户有什么风险吗?安全的吗?
After 1995, programmers in big factories were sentenced to 9 months for deleting databases
学习使用php实现无限极评论和无限极转二级评论解决方案
Vite Lerna Monorepo 项目改造初体验
How to query the database with eloquent in laravel (select)
Pyqt QWidget prohibits operations that are not current windows
Changshu science and technology applet SQL injection