本题注意点:list平整化+切片技巧实现reshape,或者借助numpy.array投机取巧,但是较慢。
def matrixReshape(nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
if len(nums)*len(nums[0]) != r*c:
return nums
flatten = [item for i in nums for item in i]
return [flatten[i*c : (i+1)*c] for i in range(r)]
# version2(我)
# import numpy as np
# return np.array(nums).reshape(r, c).tolist()
# sample:
nums =[[1,2],
[3,4]]
r, c = 1, 4
print(matrixReshape(nums, r, c))