Their thinking :
solution 1. Main diagonal inversion plus vertical inversion .
solution 2. Horizontal inversion plus main diagonal inversion .
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
""" Do not return anything, modify matrix in-place instead. """
n = len(matrix)
for i in range(n // 2):
for j in range(n):
matrix[i][j], matrix[n-i-1][j] = matrix[n-i-1][j], matrix[i][j]
# Reverse diagonally
for i in range(n):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
Their thinking :
The problem requires constant space and is solved in situ .
Use the first row and the first column of the matrix to mark 0 Rows and columns of .
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
""" Do not return anything, modify matrix in-place instead. """
m = len(matrix)
n = len(matrix[0])
flag_col0 = False
flag_row0 = False
# Traverse the first line
for i in range(m):
if matrix[i][0] == 0:
flag_col0 = True
break
# Traverse the first column
for j in range(n):
if matrix[0][j] ==0:
flag_row0 = True
break
# Use the first row and the first column , Record the rest of the matrix as 0 Rows and columns of
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] == 0:
matrix[i][0] = matrix[0][j] = 0
# Set up 0
for i in range(1, m):
for j in range(1, n):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if flag_col0:
for i in range(m):
matrix[i][0] = 0
if flag_row0:
for j in range(n):
matrix[0][j] = 0
Their thinking :
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m, n = len(mat), len(mat[0])
count = m * n
x,y= 0,0
res = []
for i in range(count):
res.append(mat[x][y])
# Move to the top right
if(x + y) % 2 ==0:
# The last column
if y == n - 1:
x += 1
# first line
elif x== 0:
y += 1
else:
x -= 1
y += 1
# Move lower left
else:
# The last line
if x == m - 1:
y += 1
# First column
elif y ==0:
x += 1
else:
x += 1
y -= 1
return res
Their thinking :
simulation
Both sides of the boundary are 1
The first i Elements of the line j For the first time i - 1 Elements of the line j - 1 and j And
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
res = []
for i in range(numRows):
row = []
for j in range(i+1):
if j ==0 or j ==i:
row.append(1)
else:
row.append(res[i-1][j-1] + res[i-1][j])
res.append(row)
return res
Their thinking :
And 118 Same logic or simulation , The difference is to output the given index row .
Use an array , Double loop traversal .
First traversal k That's ok , Then the elements at each position of each line are assigned in reverse order .
The reverse order is because res[j] = res[j-1] + res[j] .
class Solution:
# simulation
def getRow(self, rowIndex: int) -> List[int]:
res = [0] * (rowIndex + 1)
for i in range(rowIndex + 1):
for j in range(i, -1, -1):
if j==0 or j== i:
res[j] = 1
else:
res[j] = res[j-1] + res[j]
return res
Their thinking :
Set boundaries up, down, left, right , Simulate again .
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
up, down, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
res = []
while True:
for i in range(left, right + 1):
res.append(matrix[up][i])
up += 1
if up > down:
break
for i in range(up, down + 1):
res.append(matrix[i][right])
right -= 1
if right < left:
break
for i in range(right, left - 1, -1):
res.append(matrix[down][i])
down -= 1
if down < up:
break
for i in range(down, up - 1, -1):
res.append(matrix[i][left])
left += 1
if left > right:
break
return res
Their thinking :
And 54 The same way of thinking .
No, this time, I want to define a matrix , Assign a value to the matrix clockwise .
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
matrix = [[0 for _ in range(n)] for _ in range(n)]
up, down, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
index = 1
while True:
for i in range(left, right + 1):
matrix[up][i] = index
index += 1
up += 1
if up > down:
break
for i in range(up, down + 1):
matrix[i][right] = index
index += 1
right -= 1
if right < left:
break
for i in range(right, left - 1, -1):
matrix[down][i] = index
index += 1
down -= 1
if down < up:
break
for i in range(down, up - 1, -1):
matrix[i][left] = index
index += 1
left += 1
if left > right:
break
return matrix
Their thinking :
Living conditions :
Use living cells to affect other grids , Instead of traversing eight grids around each cell in turn .
Add the number in the affected grid 10, The original state of the one digit passbook grid , Ten people keep how many living grids are around it .
For example, a grid is 31, It means that there is... Around it 3 Live cells , And the previous status is 1, That is, live .
The problem requires to be solved in situ , After judging all the grids , And then change it uniformly according to the living conditions borad.
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
""" Do not return anything, modify board in-place instead. """
m=len(board)
n=len(board[0])
# Use each living point to affect other points
def affect( x, y):
for i in [x-1, x, x+1]:
for j in [y-1, y, y+1]:
if i < 0 or i >= m or j<0 or j>=n or(i==x and j ==y):
continue
board[i][j] += 10
def calculate(x, y):
value = board[x][y]
if value // 10 == 3:
board[x][y] = 1
elif value // 10 ==2 and value % 10 == 1:
board[x][y] = 1
else:
board[x][y] = 0
for i in range(m):
for j in range(n):
if board[i][j] % 10 == 1:
affect(i,j)
for i in range(m):
for j in range(n):
calculate(i,j)