第八周 周六作业

题目描述:


代码:

class Solution:
	def matrixReshape(self, nums, r,c):
		if r*c != len(nums)*len(nums[0]):
			return nums
		else:
			row = 0
			col = 0
			answer = []
			for i in range(0,r):
				temp = []
				for j in range(0,c):
					temp.append(nums[row][col])
					col = (col+1)%len(nums[0])
					if col == 0:
						row += 1
				answer.append(temp)
			return answer  
				

测试代码:

from solution import *
nums = [[1,2],
 [3,4]]
print(Solution().matrixReshape(nums, 1, 4))

测试结果:


猜你喜欢

转载自blog.csdn.net/ppjustin/article/details/80143994