Leetcode_Python 566 重塑矩阵_数组_二维数组变换

解题思路

本题主要考查二维数组的遍历,我们首先将二维数据降为一维,在进行行列的遍历赋值。

代码

class Solution(object):
    def matrixReshape(self, nums, r, c):
        """
        :type nums: List[List[int]]
        :type r: int
        :type c: int
        :rtype: List[List[int]]
        """
        mask = [[0]*c for i in range(r)]
        M = len(nums[0])
        N = len(nums)
        if M*N != r*c:
            return nums
        else:
            list1 = []
            for i in nums:
                list1 += i
            for i in range(r):
                for j in range(c):
                    mask[i][j] = list1[i*c + j]
            return mask
```@[TOC](目录)