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:

  1. The height and width of the given matrix is in range [1, 100].
  2. 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的更多相关文章

  1. Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)

    Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...

  2. LeetCode 566. Reshape the Matrix (重塑矩阵)

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  3. LeetCode 566 Reshape the Matrix 解题报告

    题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...

  4. LeetCode 566. Reshape the Matrix (C++)

    题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...

  5. leetcode 566 Reshape the Matrix 重塑矩阵

    参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...

  6. LeetCode - 566. Reshape the Matrix (C++) O(n)

    1. 题目大意 根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数.如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵.否则,重塑矩阵.其中两个矩阵中的数据顺序不变(先行后列). ...

  7. LeetCode 566. 重塑矩阵(Reshape the Matrix)

    566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...

  8. 566. Reshape the Matrix - LeetCode

    Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ...

  9. 【LeetCode】566. Reshape the Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...

随机推荐

  1. ASP.NET Core 实现用户登录验证的最低配置

    背景是在一个项目中增加临时登录功能,只需验证用户是否登录即可,所需的最低配置与实现代码如下. 在 Startup 的 ConfigureServices() 方法中添加 Authentication ...

  2. jdbc--------JdbcUtilDao 类

    2018-12-14 目标:做成一个比较通用的 sql 操作 import com.ljs.util.JDBCUtil; 类名:JdbcUtilDao 1: 更新操作, 针对任何表,增加,删除,更新操 ...

  3. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  4. [No0000145]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈2/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  5. Vue 超快速学习

    Vue 超快速学习 基础知识: 1.vue的生命周期: beforeCreate/created. beforeMount/mounted. beforeUpdate/updated. beforeD ...

  6. 【作业】用栈模拟dfs

    题意:一个迷宫,起点到终点的路径,不用递归. 题解: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...

  7. SQL之分组排序取top n

    转自:http://blog.csdn.net/wguangliang/article/details/50167283 要求:按照课程分组,查找每个课程最高的两个成绩. 数据文件如下: 第一列no为 ...

  8. angular 表单元素的验证清除问题

    项目中利用了前些时候写的弹出dialog的方式,验证方式用了控件angular-validation(http://www.cnblogs.com/FineDay/p/7255689.html) 验证 ...

  9. log4j.properties 日志文件的详细配置说明

    一.在一个web 项目中,使用tomcat 启动通常会在控制台输出出现一个警告信息: 通常为未添加 log4j.properties文件的原因. 二.下面以一个普通的maven项目为例说明一下 1. ...

  10. 2018/05/07 Linux find命令中{}的作用

    首先抛出一个问题,如果你在文件中建立了一个 -a.txt 的文件,你会怎么删除它? 我之前的回答是 rm -a.txt 实验结果很明显,不能删除. 因为在 Linux 下,会把 -a.txt 前面的 ...