Rotate Image


質問する
に答える

from collections import deque
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
q = deque()
for i in range(n):
for j in range(n):
q.append(matrix[i][j])
for col in range(n-1,-1,-1):
for row in range(n):
matrix[row][col] = q.popleft()
結果
最初は40 msでしたが、回ってから615036はずっと変わっていました...ㅇ…
ライブラリになるような気がしたので探してみました.
リストを逆シーケンスで変換し、zipを使用して垂直に読み出す方法があります.
こんな時にもZipが使えるんですよね…ぶるぶる震えるclass Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
for ind, el in enumerate(zip(*matrix[::-1])):
matrix[ind] = list(el)
Reference
この問題について(Rotate Image), 我々は、より多くの情報をここで見つけました
https://velog.io/@twinklesu914/Rotate-Image
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
for ind, el in enumerate(zip(*matrix[::-1])):
matrix[ind] = list(el)
Reference
この問題について(Rotate Image), 我々は、より多くの情報をここで見つけました https://velog.io/@twinklesu914/Rotate-Imageテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol