-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path566_ReshapetheMatrix.py
More file actions
37 lines (34 loc) · 1.08 KB
/
566_ReshapetheMatrix.py
File metadata and controls
37 lines (34 loc) · 1.08 KB
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
37
class Solution:
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
row = len(nums)
col = len(nums[0])
ans = []
if (row * col) % (c*r) == 0:
oneRow = []
for num in nums:
oneRow += num
print(oneRow)
for ind in range(0, r*c, c):
ans.append(oneRow[ind:ind+c])
else:
return nums
return ans
def main():
nums = [[1,2],[3,4]]
print(Solution().matrixReshape(nums, 1, 4))
# Output: [[1,2,3,4]]
# Explanation:
# The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix,
# fill it row by row by using the previous list.
# print(Solution().matrixReshape([[1,2],[3,4]], 2, 4))
# Output: [[1,2], [3,4]]
# Explanation:
# There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
if __name__ == '__main__':
main()