Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
题目描述
在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据不变。
已知一个由二维数组表示的矩阵,和两个正整数r(行),c(列),将这个二维数组变换为r*c的矩阵。
如果不能由原矩阵转换为r*c的矩阵就输出原矩阵,否则输出转换后的矩阵。
测试样例
Input:
nums =
[[1,2],
[3,4]]
r = 1, c = 4
Output:
[[1,2,3,4]]
nums =
[[1,2],
[3,4]]
r = 2, c = 4
Output:
[[1,2],
[3,4]]
Explanation:
原2*2矩阵不能变换为2*4矩阵,所以原样输出。
详细分析
考虑将[[1,2],[3,4]]转换为1*4的[1,2,3,4]。
首先我们需要两个循环,将原矩阵数据填入新矩阵
for(int i=0;i<r;i++){
for(int k=0;k<c;k++){
...
}
}
这里的难点是坐标的变换。它们对照关系如下:
newArr[0][0]=>oldArr[0][0]
newArr[0][1]=>oldArr[0][1]
newArr[0][3]=>oldArr[1][0]
newArr[0][4]=>oldArr[1][1]
这里我们考虑一种中间形式,先把新二维坐标转换为一维坐标,再将一维坐标转换为旧的二维坐标。比如,当我们填入newArr[0][3]时,它的一维坐标是0*c(新矩阵列数)+3,即3,然后3转化为旧的二维坐标就是old[3/原二维列数][3%原二维列数]
代码实现
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
//corner case for empty 2-dimension array
if(nums.size()==0){
return nums;
}
if(nums[0].size()==0){
return nums;
}
//illegal case
if(nums.size()*nums[0].size()!=r*c){
return nums;
}
std::vector<std::vector<int>> dv;
for(int i=0;i<r;i++){
std::vector<int> v;
for(int k=0;k<c;k++){
int p=i*c+k;
int idx1=p/nums[0].size();
int idx2=p%nums[0].size();
v.push_back(nums[idx1][idx2]);
}
dv.push_back(v);
}
return dv;
}
};
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)的更多相关文章
- 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 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++) O(n)
1. 题目大意 根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数.如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵.否则,重塑矩阵.其中两个矩阵中的数据顺序不变(先行后列). ...
- leetcode 566 Reshape the Matrix 重塑矩阵
参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...
- 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 ne ...
- [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 o ...
随机推荐
- strcmp与strncmp的区别
================== strcmp与strncmp都是用来比较字符串的,区别在于能否比较指定长度字符串. strcmp C/C++函数,比较两个字符串 设这两个字符串为str1,s ...
- 【OpenCV】图像代数运算:平均值去噪,减去背景
代数运算,就是对两幅图像的点之间进行加.减.乘.除的运算.四种运算相应的公式为: 代数运算中比较常用的是图像相加和相减.图像相加常用来求平均值去除addtive噪声或者实现二次曝光(double-ex ...
- Facebook开源的JavaScript库:React
React是Facebook开源的JavaScript库,采用声明式范例,可以传递声明代码,最大限度地减少与DOM的交互. React是Facebook开源的JavaScript库,用于构建UI.你可 ...
- 问题:Oracle to_date;结果:oracle常用的时间格式转换
oracle常用的时间格式转换 1:取得当前日期是本月的第几周 SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; T ...
- 问题:C#打开一个文本文档往里面写数据,没有就新建文档 ;结果:c#FileStream文件读写(转)
FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字节的方法,但经常使用StreamReader或 StreamWriter执行这些功能.这是因为FileStream ...
- NDK 编译报错:request for member 'FindClass' in something not a structure or union
ndk编译 xx.c文件时一直报下面的错误: ”request for member 'FindClass' in something not a structure or union ...” 原因 ...
- [Python Study Notes]饼状图绘制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- Comparatable接口和Comparator接口的使用与区别
这篇博文可以为你解决的问题如下: 什么是自然排序 Collections.sort()与Arrays.sort()的异同点 Comparatable接口和Comparator接口各自的排序依据(理论讲 ...
- Select2下拉选项库 部分积累
用了这么久的Select2插件,也该写篇文章总结总结. 在我的印象里Select2有2个版本,最新版本有一些新的特性,并且更新了一下方法参数,比最初版本要好看一些,本文针对新版本. 官网:http:/ ...
- iframe自适应高度(转)
iframe自适应高度 (2013-04-23 17:29:49) 标签: iframe 高度 自适应 js 杂谈 分类: 网页制作 有时候我们的网站需要引入其他网站的东西,比如评论,这时候就需要使用 ...