[LeetCode&Python] Problem 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 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.
class Solution:
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
m=len(nums)
n=len(nums[0]) if m*n!=r*c:
return nums
else:
ans=[]
for i in range(r):
ans0=[None]*c
for j in range(c):
if (i*c+j+1)%n!=0:
ans0[j]=nums[int((i*c+j+1)//n)][(i*c+j+1)%n-1]
else:
ans0[j]=nums[int((i*c+j+1)//n)-1][n-1]
ans.append(ans0)
return ans
[LeetCode&Python] Problem 566. Reshape the Matrix的更多相关文章
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- 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/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
- 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
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
- 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- 566. Reshape the Matrix矩阵重排
[抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
随机推荐
- SetCommMask
SetCommMask 用途:设置串口通信事件 原型:BOOL SetCommMask(HANDLE hFile, //标识通信端口的句柄 DWORD dwEvtMask ...
- 《剑指offer》第三十一题(栈的压入、弹出序列)
// 面试题31:栈的压入.弹出序列 // 题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是 // 否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1.2.3.4. / ...
- Codeforces D - Ithea Plays With Chtholly
D - Ithea Plays With Chtholly 思路:考虑每个位置最多被替换c/2次 那么折半考虑,如果小于c/2,从左往右替换,大于c/2总右往左替换,只有小于这个数(从左往右)或者大于 ...
- eclipse启动时弹出Failed to create the Java Virtual Machine
eclipse启动时弹出Failed to create the Java Virtual Machine 一.现象 今天装eclipse的时候出现Failed to create the Java ...
- zabbix3.0.4 配置邮件报警
试验环境: LAMP环境 (LNMP环境已经成功了,为了避免干扰,我另一台LAMP主机) ### 我在做实验之前,作了时间同步,不知道这个有木有影响,一起说一下吧! yum -y install nt ...
- 字符串 dfs
1222: FJ的字符串 [水题] 时间限制: 1 Sec 内存限制: 128 MB 提交: 52 解决: 9 状态 题目描述 FJ在沙盘上写了这样一些字符串: A1 = “A” A2 = “ ...
- Coconuts, Revisited(递推+枚举+模拟)
Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...
- OAF页面集成条形码或者二维码
OAF页面集成条形码 OAF生成条形码 OAF页面集成二维码跟这个类似,生成二维码需要以下jar包,zxing-core.jar, zxing-javase.jar,可自行去maven下载. 代码如下 ...
- 给构造函数(constructor)创建对象(object)
(来源http://www.cnblogs.com/dongjc/p/5179561.html) javascript是一种“基于prototype的面向对象语言“,与java有非常大的区别,无法通过 ...
- SQL Server 调优系列进阶篇 - 如何维护数据库索引
前言 上一篇我们研究了如何利用索引在数据库里面调优,简要的介绍了索引的原理,更重要的分析了如何选择索引以及索引的利弊项,有兴趣的可以点击查看. 本篇延续上一篇的内容,继续分析索引这块,侧重索引项的日常 ...