-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathalgo566.py
34 lines (31 loc) · 863 Bytes
/
algo566.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
class Solution(object):
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
if nums == []:
return []
nums_r = len(nums)
nums_c = len(nums[0])
if r * c != nums_r * nums_c:
return nums
row = 0
nnums = []
for x in range(r):
nnums.append([])
count=0
for x in range(len(nums)):
for y in range(len(nums[x])):
count+=1
if count==c:
nnums[row].extend([nums[x][y]])
row += 1
count=0
else:
nnums[row].extend([nums[x][y]])
return nnums
sol=Solution()
print(sol.matrixReshape([[1,2,3,4]],2,2))