Noob solution


  • 0
    C
        def matrixReshape(self, nums, r, c):
            """
            :type nums: List[List[int]]
            :type r: int
            :type c: int
            :rtype: List[List[int]]
            """
            
            # flatten?
             
            rows = len(nums)       # rows = 2
            cols = len(nums[0])    # columns = 2
            
            if rows * cols < r * c:
                return nums
            
            res = []
            new_list = []           # [1,2,3,4]
            for i in range(rows):   # O(n^2)
                for j in range(cols):
                    new_list.append(nums[i][j])
            
            new_list.reverse()      # [4,3,2,1]
            
            for i in range(r):
                sub_list = []
                for j in range(c):
                    sub_list.append(new_list.pop())
                res.append(sub_list)
                    
            return res
    

Log in to reply
 

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