LeetCode 566 Reshape the Matrix 解题报告
题目要求
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
You're given a matrix represented by a two-dimensional array, and two positive integers r and crepresenting the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
题目分析及思路
给定一个二维矩阵和想要得到的新矩阵的行数和列数,返回新矩阵。新矩阵要被原矩阵的所有元素填满,且如果得不到新矩阵,则返回原矩阵。可以将原矩阵的元素放在一个列表中,然后根据所给行数和列数对该列表进行拆分。
python代码
class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
rows, cols = len(nums), len(nums[0])
if rows * cols != r * c:
return nums
else:
matrix_list = []
for row in nums:
matrix_list.extend(row)
new_matrix1 = []
new_matrix2 = []
for idx in range(r):
for i in range(0+idx*c,c+idx*c):
new_matrix1.append(matrix_list[i])
new_matrix2.append(new_matrix1)
new_matrix1 = []
return new_matrix2
LeetCode 566 Reshape the Matrix 解题报告的更多相关文章
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- LeetCode 566. Reshape the Matrix (重塑矩阵)
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- LeetCode 566. Reshape the Matrix (C++)
题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...
- 【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...
- 【LeetCode】54. Spiral Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 【LeetCode】867. Transpose Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https ...
- LeetCode: Search a 2D Matrix 解题报告
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- leetcode 566 Reshape the Matrix 重塑矩阵
参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...
随机推荐
- docker启动centos7后sudo不能使用
docker启动centos7后sudo不能使用 过程 使用docker -it xxx /bin/sh进入centos镜像,然后安装了docker,想使用systemctl start docker ...
- Xmpp获取离线消息
文章只是选取了其中一段,无XMPP基础的人可能看起来有点复杂; 假设我们注册了一个用户,用户名叫shimiso,那么我们如何让shimiso这个用户一登陆就取到离线消息呢? PPConnection. ...
- spring batch中用到的表
1,批量表的前缀:{prefix}来自类AbstractJdbcBatchMetadataDao中的变量DEFAULT_TABLE_PREFIX 2,{prefix}job_execution:存放j ...
- fputc和putc和putchar函数的用法
功 能: 输出一字符到指定流中 putc()与fputc()等价.不同之处为:当putc函数被定义为宏时,它可能多次计算stream的值. 关于fputc(): 原型:int fputc(char c ...
- java指纹识别+谷歌图片识别技术_源代码
主类: import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; public c ...
- [PGM] Variable elimination and Belief propagation
内容范围如题.Lec 08-11的内容:https://www.youtube.com/watch?v=Qa04kw1gKHk&index=36&list=PLQiVpyxVlLkbp ...
- [Converge] Gradient Descent - Several solvers
solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’ Algorithm to use in the op ...
- 如何处理MySQL每月5亿的数据
第一阶段:1,一定要正确设计索引2,一定要避免SQL语句全表扫描,所以SQL一定要走索引(如:一切的 > < != 等等之类的写法都会导致全表扫描)3,一定要避免 limit 100000 ...
- 《objective-c基础教程》学习笔记(五)—— 继承方法
在上一篇博文中,我们将原先的纯C语言代码,编写成了用Objective-C(后面直接缩写成OC)的写法.使得代码在易读性上有明显提升,结构也更清晰.同时,也对面向对象的概念有了进一步的介绍和加深. 但 ...
- 解决windows和ubuntu之间粘贴复制失效
vmware12中ubuntu15.10 16.10的vmware tools失效,导致不能复制粘贴文件以及自动适应窗口分辨率新安装或异常关机和重新划分分区导致的vmware tools失效,不能复制 ...