题目
代码
class Solution:
def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:
ans=[]
if m*n!=len(original):
return []
for i in range(m):
temp=[]
for j in range(n):
temp.append(original[i*n+j])
ans.append(temp)
return ans
【写法2】
class Solution:
def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:
return [original[i: i + n] for i in range(0, len(original), n)] if len(original) == m * n else []