566. Reshape the Matrix

来源:互联网 发布:美国仓库淘宝网 编辑:程序博客网 时间:2024/06/09 17:14
class Solution(object):
    def matrixReshape(self, nums, r, c):
        """
        :type nums: List[List[int]]
        :type r: int
        :type c: int
        :rtype: List[List[int]]
        """
        row=len(nums)
        colum=len(nums[0])
        if row*colum!=r*c:
            return nums
        else:
            tmp=[num for row in nums for num in row]
            newarray=[[0 for j in range(c)]for i in range(r)]
            for i in range(r):
                for j in range(c):
                    newarray[i][j]=tmp[i*c+j]
            return newarray