[LeetCode] 566. Reshape the Matrix_Easy
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 c representing 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.
Example 1:
Input:
nums =
[[1,2],
[3,4]]
r = 1, c = 4
Output:
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
Example 2:
Input:
nums =
[[1,2],
[3,4]]
r = 2, c = 4
Output:
[[1,2],
[3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
Note:
- The height and width of the given matrix is in range [1, 100].
- The given r and c are all positive.
利用queue, 如果lr*lc == r*c的话.
Code
class Solution:
def reshapeMatrix(self, nums, r, c):
lr, lc = len(nums), len(nums[0])
if lr*lc != r*c: return nums
ans, queue = [[0]*c for _ in range(c)], collections.deque()
for i in range(lr):
for j in range(lc):
queue.append(nums[i][j])
for i in range(r):
for j in range(c):
ans[i][j] = queue.popleft()
return ans
[LeetCode] 566. Reshape the Matrix_Easy的更多相关文章
- 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 解题报告
题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
- 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 566 Reshape the Matrix 重塑矩阵
参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...
- LeetCode - 566. Reshape the Matrix (C++) O(n)
1. 题目大意 根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数.如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵.否则,重塑矩阵.其中两个矩阵中的数据顺序不变(先行后列). ...
- LeetCode 566. 重塑矩阵(Reshape the Matrix)
566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...
- 566. Reshape the Matrix - LeetCode
Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ...
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
随机推荐
- sublime3 配置go的开发环境
配置go的环境变量:https://studygolang.com/articles/4910 配置sublime:https://studygolang.com/articles/4938
- CodeForces - 748E (枚举+脑洞)
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
This function has none of Deterministic,no sql,or reads sql data in its declaration and binary loggi ...
- 将音乐生成波浪图形,JavaScript Html5
x 省略废话(N+)... Windows Media Palyer中的经典波浪形 自己也行动手做一个,最好是JavaScript实现的, 搜索到了资源部分关键词"HTML5 频谱" ...
- CentOS-6.9安装配置JDK-7
CentOS-6.9安装配置JDK-7 安装说明 系统环境:centos-6.9安装方式:rpm安装 软件:jdk-7u79-linux-x64.rpm下载地址:http://download.ora ...
- Python:多线程
据廖雪峰老师的学习文档介绍,高级语言通常都内置多线程的支持,Python也不例外,并且,Python的线程是真正的Posix Thread,而不是模拟出来的线程. Python的标准库提供了两个模块: ...
- pause
https://stackoverflow.com/questions/37063700/mm-pause-usage-in-gcc-on-intel?utm_medium=organic&u ...
- [grub2] grub2修改启动顺序
https://wiki.centos.org/HowTos/Grub2#head-535f476a61e62f24bc150c73f7e0816f85345f46 1, 查看所有的entry [ro ...
- 【转】escape()、encodeURI()、encodeURIComponent()区别详解
escape().encodeURI().encodeURIComponent()区别详解 原文链接:http://www.cnblogs.com/tylerdonet/p/3483836.html ...
- day2_python基础
1.变量: 用来存东西的,左边是名字,右边是值 2.python中的单引号.双信号.三引号 单引号和双引号和三引号没什么区别,用哪个都可以,如果定义字符串里面如果有单引号,则外面用双引号;如果字符串里 ...