-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreshape.py
More file actions
28 lines (25 loc) · 755 Bytes
/
reshape.py
File metadata and controls
28 lines (25 loc) · 755 Bytes
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
class Solution(object):
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
total = r*c
all_elements = []
# all nums
for num in nums:
all_elements.extend(num)
result = []
if total > len(all_elements):
return nums
else:
for i in range(r):
current = []
for j in range(c):
if j < len(all_elements):
current.append(all_elements[j])
result.append(current)
all_elements = all_elements[c:]
return result