-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreshape.py
36 lines (31 loc) · 922 Bytes
/
reshape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Reshape without intermediate single dimension array
from typing import List
def matrixReshape(mat: List[List[int]], r: int, c: int) -> List[List[int]]:
m = len(mat)
n = len(mat[0])
res = list()
if m * n != r * c:
return mat
p = q = 0
for i in range(r):
for j in range(c):
if p < m:
if q < len(mat[p]):
if i > len(res) - 1:
res.append([mat[p][q]])
else:
res[i].append(mat[p][q])
q += 1
else:
p += 1
q = 0
if i > len(res) - 1:
res.append([mat[p][q]])
else:
res[i].append(mat[p][q])
q += 1
return res
mat = [[1, 2, 4], [3, 4, 5]]
r = 3
c = 2
print(matrixReshape(mat, r, c))