祖传的手艺不想丢了,所以按顺序写一个leetcode的题解。计划每日两题,争取不卡题吧。
566.重塑矩阵https://leetcode-cn.com/problems/reshape-the-matrix/
比较基础的题目,首先判断m * n是否等于r * c,若不等于那么就无法reshape,直接返回原矩阵即可。若可以变换,那么逐行逐列遍历原矩阵中的数字,将其加入到答案矩阵中,当答案矩阵中当前行的数字达到c个时就另起一行。
最后附上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]]
"""
m = len(nums)
n = len(nums[0])
if m * n != r * c:
return nums
res = []
cnt = 0
for i in range(m):
for j in range(n):
if cnt == 0:
res.append([])
cnt = 0
res[-1].append(nums[i][j])
cnt += 1
if cnt == c:
cnt = 0
return res