python
class Solution:
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
r_count = len(nums)
c_count = len(nums[0])
if r_count * c_count != r * c:
return nums
else:
t = []
for i in range(r_count):
for j in range(c_count):
t.append(nums[i][j])
rList = []
m = 0
for k in range(r):
rList.append(t[m:m+c])
m += c
return rList
思路:
将原来矩阵中的元素取出,放到一个list中,然后再从重塑添加到新的矩阵中。