【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/reshape-the-matrix/description/
题目描述
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.
题目大意
实现Matlab的reshape函数,就是把原来的数组逐行遍历重新改成r行,c列,如果不能实现的话就是返回原来的数组。
解题方法
变长数组
Python的list是可变的,那么一个简单的想法就是使用可变的List作为新的一行,如果新的一行的元素个数等于题目要求的列数,那么就新建一行。把每行的结果放到返回的列表中即可。
时间复杂度是O(mn),空间复杂度是O(1).没有额外空间
class Solution(object):
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
M, N = len(nums), len(nums[0])
if M * N != r * c:
return nums
res = []
row = []
for i in range(M):
for j in range(N):
row.append(nums[i][j])
if len(row) == c:
res.append(row)
row = []
return res
求余法
这个做法是由位图法激发出来的,使用一个变量count保存现在已经有了的元素数量,那么直接可以使用res[count / c][count % c]确定轮到了的元素的位置。这个方法需要把数组提前构造好。
时间复杂度是O(mn),空间复杂度是O(1).没有额外空间
class Solution(object):
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
M, N = len(nums), len(nums[0])
if M * N != r * c:
return nums
res = [[0] * c for _ in range(r)]
count = 0
for i in range(M):
for j in range(N):
res[count / c][count % c] = nums[i][j]
count +=1
return res
维护行列
上面的方法每次需要通过除法和求余来确定新的位置,事实上是比较耗时的。更快的方法就是维护行和列的变量,在遍历的过程中更新行和列,这样就可以对应放入的位置了。
时间复杂度是O(mn),空间复杂度是O(1).没有额外空间
class Solution(object):
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
M, N = len(nums), len(nums[0])
if M * N != r * c:
return nums
res = [[0] * c for _ in range(r)]
row, col = 0, 0
for i in range(M):
for j in range(N):
if col == c:
row += 1
col = 0
res[row][col] = nums[i][j]
col += 1
return res
相似题目
参考资料
https://leetcode.com/articles/reshape-the-matrix/
日期
2018 年 11 月 1 日 —— 小光棍节
【LeetCode】566. Reshape the Matrix 解题报告(Python)的更多相关文章
- 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 矩阵变形(数组,模拟,矩阵操作)
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】54. Spiral Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 【LeetCode】867. Transpose Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https ...
- 【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 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 ...
随机推荐
- xshell的快捷复制粘贴设置
今天试着用xshell连接Linux,运行一些命令的时候想快点复制粘贴实现效率,却发现还要右键选择复制,再右键选择粘贴,很是麻烦. 看了一下xshell的设置,其实可以自己设置成快捷方式 以xshel ...
- Docker的基本使用及DockerFile的编写
前言: 最近在准备面试,在复习到Docker相关内容时,想写一些东西分享给大家然后加深一下自己的印象,有了这篇随笔. Docker的简介: docker从文件系统.网络互连到进程隔离等等,极大的简化了 ...
- Levenshtein莱文斯坦算法在项目中的应用
简介 根据维基百科的描述,在信息理论.语言学和计算机科学中,莱文斯坦距离是一个测量两个序列之间差异的字符串度量.非正式地,两个单词之间的莱文斯坦距离是将一个单词改变为另一个单词所需的最小单字符编辑次数 ...
- SpringCloud微服务实战——搭建企业级开发框架(三十一):自定义MybatisPlus代码生成器实现前后端代码自动生成
理想的情况下,代码生成可以节省很多重复且没有技术含量的工作量,并且代码生成可以按照统一的代码规范和格式来生成代码,给日常的代码开发提供很大的帮助.但是,代码生成也有其局限性,当牵涉到复杂的业务逻辑 ...
- 连接查询条件在on后面和条件在where后面
emp表结构如下: dept表结构如下: 内连接 条件语句放在on 后面和 where 结果对于inner join结果是一样的 但对于left join 结果会产生不一样 这种现象也比较好理解,如果 ...
- Fragment放置后台很久(Home键退出很长时间),返回时出现Fragment重叠解决方案
后来在google查到相关资料,原因是:当Fragment长久不使用,系统进行回收,FragmentActivity调用onSaveInstanceState保存Fragment对象.很长时间后,再次 ...
- OkHttp3 使用
导入 compile 'com.squareup.okhttp3:okhttp:3.3.0' GET请求 String url = "https://www.baidu.com/" ...
- Copy elision in C++
Copy elision (or Copy omission) is a compiler optimization technique that avoids unnecessary copying ...
- dom4j解析XML学习
原理:把dom与SAX进行了封装 优点:JDOM的一个智能分支.扩充了其灵活性增加了一些额外的功能. package com.dom4j.xml; import java.io.FileNotFoun ...
- 【Linux】【Basis】CentOS启动流程
1. 基础概念 1.1 Linux系统的组成部分:内核+根文件系统 内核:进程管理.内存管理.网络协议栈.文件系统.驱动程序.安全功能 IPC:In ...