#566. Reshape the Matrix

2017-05-02  本文已影响192人  Double_E

https://leetcode.com/problems/reshape-the-matrix/#/hints

image.png

思路

Python

class Solution(object):
    def matrixReshape(self, nums, r, c):
        """
        :type nums: List[List[int]]
        :type r: int
        :type c: int
        :rtype: List[List[int]]
        """
        rr = len(nums)
        cc = len(nums[0])
        if rr * cc != r * c:
            return nums
        nums_new = []
        for i in range(r):
            tmp = []
            for j in range(c):
                k = i * c + j
                tmp.append(nums[k // cc][k % cc])
            nums_new.append(tmp)
        return nums_new
上一篇 下一篇

猜你喜欢

热点阅读