Matrix
Matrix
0.1 Set-matrix-zeroes
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s.
You must do it in place.
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2:
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
[ ]: class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
# Note always choose matrix M*N size, below is example where matrix is␣
↪ n*m
n = len(matrix)
m = len(matrix[0])
print(n)
c00 = matrix[0][0]
first_row_flag, first_column_flag = 1, 1
1
if matrix[i][j] == 0:
matrix[i][0] = matrix[0][j] = 0
# filling row to 0
for i in range(1, n):
if matrix[i][0] == 0:
for j in range(1, m):
matrix[i][j] = 0
# filling column to 0
for j in range(1, m):
if matrix[0][j] == 0:
for i in range(1, n):
matrix[i][j] = 0
# filling
if first_column_flag == 0:
for i in range(n):
matrix[i][0] = 0
if first_row_flag == 0:
for j in range(m):
matrix[0][j] = 0
0.2 Spiral-matrix
Given an m x n matrix, return all elements of the matrix in spiral order.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7]
[ ]: class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
n = len(matrix) # number of row
m = len(matrix[0]) # number of column
r1, r2, c1, c2 = 0, n-1, 0 , m-1
# print(r1, r2, c1, c2 )
result = []
while r1 <= r2 and c1 <= c2:
print(r1, r2, c1, c2 )
# print the top row & reduce r1
if r1 <= r2:
for j in range(c1, c2+1, 1):
result.append(matrix[r1][j])
r1 = r1+1
2
if r1 <= r2:
for i in range(r1, r2+1, 1):
result.append(matrix[i][c2])
c2 = c2-1
return result
0.3 Rotate-image
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix
directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output:
[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
[ ]: class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
print(n)
for i in range(n):
for j in range(i,n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
print(matrix)
for j in range(n//2):
for i in range(n):
matrix[i][j] , matrix[i][n-j-1] = matrix[i][n-j-1], matrix[i][j]
3
0.4 Word-search
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are
horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example 1:
Input: board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
Output: true
Example 2:
Input: board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “SEE” Output:
true
[ ]: class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
m = len(board)
n = len(board[0])
path = []
def dfs(r,c,i):
if i == len(word):
return True
if (r < 0 or c < 0 or
r >= m or c >= n or
board[r][c] != word[i] or
(r,c) in path):
return False
path.append((r,c))
result = (dfs(r,c-1,i+1) or
dfs(r-1,c,i+1) or
dfs(r+1,c,i+1) or
dfs(r,c+1,i+1))
path.remove((r,c))
return result
for j in range(m):
for k in range(n):
if dfs(j, k, 0):
return True
return False