题目要求

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

转载于:https://www.cnblogs.com/yao1996/p/10474709.html

LeetCode 566 Reshape the Matrix 解题报告相关推荐

  1. LeetCode 566. Reshape the Matrix

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

  2. leetcode 566. Reshape the Matrix | 566. 重塑矩阵(Java)

    题目 https://leetcode.com/problems/reshape-the-matrix/ 题解 考矩阵变换,简单题,不多说,直接上代码. class Solution {public ...

  3. Leetcode PHP题解--D53 566. Reshape the Matrix

    2019独角兽企业重金招聘Python工程师标准>>> D53 566. Reshape the Matrix 题目链接 566. Reshape the Matrix 题目分析 给 ...

  4. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  5. [LeetCode]844. Backspace String Compare 解题报告(C++)

    [LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...

  6. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  7. leetcode讲解--566. Reshape the Matrix

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

  8. python矩阵reshape_[LeetCode Python3]566. Reshape the Matrix(重塑矩阵)

    在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的 ...

  9. [Leetcode] 74. Search a 2D Matrix 解题报告

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

最新文章

  1. python基础教程:函数作用域
  2. boost::fibers::launch::post的测试程序
  3. mysql 复制表耗时_聊一下mysql的表复制
  4. 【Linux】一步一步学Linux——at命令(133)
  5. db2 10.5 64位 linux,db2 10.5 使用列式存储
  6. 详解Objective-C消息传递机制
  7. 入门干货:Python操作Word文件经验分享
  8. ImageNet2012数据集完整版百度云下载
  9. Oracle序列生成器
  10. python点击屏幕_Python屏幕操作
  11. jquery ui accordion的详细参数
  12. JAVA日期格式化大写YYYY-MM-dd和小写yyyy-MM-DD的坑
  13. 旷视科技科创板IPO上会在即,毫不吝啬研发投入
  14. java判断闰年中闰月_编程序:计算某年某月有多少天(区分闰年和闰月)?怎么编?...
  15. 苹果手机在哪里清理内存_iphone苹果手机内存不足之清理大法
  16. springboot计算机专业毕业设计优秀级别最新题目
  17. 光耦w314的各引脚图_P184光耦电路作用,光电晶体管隔离24v输出电路
  18. linux7系统改成中文,把CentOS 7系统显示语言从中文修改成英文的方法
  19. 在windows下安装yii2框架
  20. A4988驱动步进电机

热门文章

  1. mysqlierror php_php操作mysqli(示例代码)
  2. 华为任职资格_华为采购总部专业任职资格标准|
  3. 计算机网络对口题目,2011-2015计算机对口升学网络试题汇总.doc
  4. python 自定义类(特殊方法)
  5. LeetCode 756. 金字塔转换矩阵(回溯)
  6. LeetCode 836. 矩形重叠
  7. LeetCode 第 186 场周赛(1060/3107,前34.1%)
  8. LeetCode 1266. 访问所有点的最小时间(数学)
  9. 上传图片自动加水印html,html5上传多个文件并添加水印 实例源码
  10. 链接聚合是将一组物理接口_500字描述华为VLAN聚合工作原理 你看懂了吗?