python - reshape matrix


  • 0
    S
    class Solution(object):
        def matrixReshape(self, nums, r, c):
            """
            :type nums: List[List[int]]
            :type r: int
            :type c: int
            :rtype: List[List[int]]
            """
            numlist = []
            for row in nums:
                numlist += row
            
            if len(numlist) < r*c:
                return nums
            
            newnums = []
            for i in range(r):
                newrow = []
                for j in range(c):
                    newrow.append(numlist.pop(0))
                newnums.append(newrow)
            
            return newnums
    

Log in to reply
 

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