2025-07-30 14:36:45 +0000 UTC
Reshape the Matrix
Categories:
Links
Code
class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if len(mat) * len(mat[0]) != r * c:
return mat
new_mat = [[None] * c for _ in range(r)]
new_row = 0
new_col = 0
for row in mat:
for col in row:
if new_col == c:
new_col = 0
new_row += 1
new_mat[new_row][new_col] = col
new_col += 1
return new_mat