Simple 4 lines python solution beats 94% python solution


  • -1
    D

    class Solution(object):
    def matrixReshape(self, nums, r, c):
    """
    :type nums: List[List[int]]
    :type r: int
    :type c: int
    :rtype: List[List[int]]
    """
    mid = []
    for x in nums:
    mid.extend(x)
    return [mid[ic:ic+c] for i in range(r)] if len(mid) == r*c else nums


  • 0
    D

    Something went wrong in your formatting.

    def matrixReshape(self, nums, r, c):
        """
        :type nums: List[List[int]]
        :type r: int
        :type c: int
        :rtype: List[List[int]]
        """
        mid = []
        for x in nums:
            mid.extend(x)
        return [mid[i*c:i*c+c] for i in range(r)] if len(mid) == r*c else nums

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.