3 Python sol. based on list comprehension, generator, and index transform 80%+ [ With explanation ]
3 Python sol. based on list comprehension, generator, and index transform.\n\n---\n\nMethod_#1: based on list comprehension\n\n\nclass Solution:\n    def matrix
3 Python sol. based on list comprehension, generator, and index transform.\n\n---\n\nMethod_#1: based on list comprehension\n\n```\nclass Solution:\n    def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n        \n        ori_rows, ori_cols = len(nums), len(nums[0])\n        \n        if ori_rows * ori_cols != r * c or (ori_rows, ori_cols) == (r, c):\n            # Quick response:\n            # \n            # mismatch of total number of elements\n            # or\n            # dimension is the same as original\n            \n            return nums\n        \n        else:\n            \n            # flatten nums to 1D array\n            flatten_array = [ element for rows in nums for element in rows ]\n            \n            # construct  reshape_arr by list comprehension\n            reshape_arr = [ flatten_array[ y*c : y*c+c ] for y in range(r) ]\n            \n            return reshape_arr\n```\n\n---\n\nMethod_#2: based on generator\n\n```\nclass Solution:\n    \n    def element_generator( self, matrix ):\n    \n        rows, cols = len( matrix), len( matrix[0])\n        \n        for index_serial in range( rows*cols):\n            y = index_serial // cols\n            x = index_serial % cols\n            yield matrix[y][x]\n    \n        \n\n    \n    def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n        \n        ori_rows, ori_cols = len(nums), len(nums[0])\n        \n        if ori_rows * ori_cols != r * c or (ori_rows, ori_cols) == (r, c):\n            # Quick response:\n            # mismatch of total number of elements\n            # or\n            # dimension is the same as original\n            \n            \n            return nums\n        \n        else:\n            \n            \n            reshape_arr = [[ 0 for x in range (c)] for y in range(r) ]\n            \n            element_gen = self.element_generator( nums )\n            \n            for y in range(r):\n                for x in range(c):\n                    \n                    reshape_arr[y][x] = next(element_gen)\n            \n            \n            return reshape_arr\n```\n\n---\n\nMethod_#3: based on index transform\n\n```\nclass Solution:\n\n    def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n        \n        ori_rows, ori_cols = len(nums), len(nums[0])\n        \n        if ori_rows * ori_cols != r * c or (ori_rows, ori_cols) == (r, c):\n            # Quick response:\n            # mismatch of total number of elements\n            # or\n            # dimension is the same as original\n            \n            \n            return nums\n        \n        else:\n            \n            reshape_arr = [[ 0 for x in range (c)] for y in range(r) ]\n            \n            for y in range(r):\n                for x in range(c):\n                    \n                    # index transform\n                    serial_index = y * c + x\n\n                    src_y = serial_index // ori_cols\n                    src_x = serial_index % ori_cols\n                    \n                    reshape_arr[y][x] = nums[src_y][src_x]\n            \n            \n            return reshape_arr\n```
['Math', 'Python', 'Python3']