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 ...
随机推荐
- RSA加密和解密工具类
import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import java.security.*; i ...
- 截图工具(window 10 和Mac OSX)
Win10上截图 1.使用系统截图工具 所有程序中可以看到 通过win+R,打开运行,输入"SnippingTool" 文件位于: C:\Windows\System32\Sn ...
- 阿里云 FTP 无法读取目录问题
安全组除了添加制定 FTP端口外 需要加入再添加1024/65535端口 放行策略,ftp被动模式需要随机开一个此范围端口进行传输 添加安全组规则参考文档:https://help.aliyun.co ...
- Jenkins这种构建工具,一般都是内部使用,所以外部基本上不能访问
类似于Jenkins这种构建工具,一般都是内部使用,所以外部基本上不能访问,也可以隔绝外部黑客的入侵等.直接暴露外部是非常不安全的,特别是没有什么安全验证,容易被别人入侵做一些非法的事情! 所以,希望 ...
- not in 的优化
//---------------------- 建表1 ---------------------- create table TESTTABLE( id1 VARCHAR2(12), name V ...
- 内存溢出OutOfMemory
https://blog.csdn.net/hzy38324/article/details/76719105 https://blog.csdn.net/u010833547/article/det ...
- js中的try/catch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- svn eclipse链接
先下载site-1.8.22.zip 安装包 然后 在D:\software\eclipse\dropins 目录下新建 svn文件夹 把下载的文件解压到该文件夹下 ,*.xml 删除 不需要 只要 ...
- distri.lua线程间通信的设计
首先简单介绍下distri.lua中的线程设计方案. distri.lua提供一个API函数fork用于创建新的C线程,这个C线程运行独立的lua虚拟机,为了在各线程之间通信 每个线程都会创建一个ch ...
- Android Studio开发第二篇创建新项目
创建新项目很简单,File-New-New Project,这个没什么好说的跟Eclipse都差不都. 第二步SDK选择,有手机平板还有Wear,TV,汽车Auto,谷歌眼镜等几个种平台,这里就先选择 ...