Created
April 1, 2021 05:33
-
-
Save liyunrui/28ead2daf9469f6a5cef598e9017e4a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def rotate(self, matrix: List[List[int]]) -> None: | |
""" | |
Do not return anything, modify matrix in-place instead. | |
""" | |
m = len(matrix) | |
n = len(matrix[0]) | |
row_b = 0 # 0-> 1 | |
row_e = m-1 # 2->1 | |
col_b = 0 | |
col_e = n-1 | |
if n == 1: | |
return | |
while row_b <= row_e and col_b <= col_e: | |
for i in range(n-1): | |
tmp = matrix[row_b][col_b+i] | |
matrix[row_b][col_b+i] = matrix[row_e-i][col_b] | |
matrix[row_e-i][col_b] = matrix[row_e][col_e-i] | |
matrix[row_e][col_e-i] = matrix[row_b+i][col_e] | |
matrix[row_b+i][col_e] = tmp | |
row_b+=1 | |
row_e-=1 | |
col_b+=1 | |
col_e-=1 | |
n -= 2 | |
def rotate(self, matrix: List[List[int]]) -> None: | |
def transpose(mat): | |
n = len(mat) | |
for i in range(n): | |
for j in range(i, n): | |
mat[i][j], mat[j][i] = mat[j][i], mat[i][j] | |
def reverse(nums): | |
l = 0 | |
r = len(nums)-1 | |
while l < r: | |
nums[l], nums[r] = nums[r], nums[l] | |
l+=1 | |
r-=1 | |
n = len(matrix) | |
if n == 1: | |
return | |
transpose(matrix) | |
for i in range(n): | |
reverse(matrix[i]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment