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 矩阵变形(数组,模拟,矩阵操作)的更多相关文章

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

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

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

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

  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++) O(n)

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

  5. leetcode 566 Reshape the Matrix 重塑矩阵

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

  6. 566. Reshape the Matrix - LeetCode

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

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

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

  8. 【leetcode】566. Reshape the Matrix

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

  9. [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 ...

随机推荐

  1. rails登录后跳转到登录前的路径

    # 重定向到存储的地址或默认地址 def redirect_back_or(default) redirect_to(session[:forwarding_url] || default) sess ...

  2. Java 的标识符

    标识符: 在写代码的时候为了增强代码的阅读性会自定义很多名字,如:类名.方法名.变量名等 这种名称成为标识符 标识符命名规则: 由字母(可以是中文).数字.下划线.$ 组成,但不能以数字开头 大小写敏 ...

  3. How to clear fmadm log or FMA faults log (ZT)

    Here are the step by step of clearing the FMA faults on most of Oracle/Sun server. Work perfectly on ...

  4. Android CTS

    1.什么是CTS CTS是google制定的兼容性测试包(Compatibility Test Suite),只有通过CTS测试的设备才有可能获得Android的商标和享受Android Market ...

  5. 异常 android.content.res.Resources$NotFoundException: String resource ID #0x61

    09-09 16:08:41.554: E/Weaver(13140):09-09 16:08:41.554: E/Weaver(13140): android.content.res.Resourc ...

  6. UAC(User Agent Client) 和 UAS(User Agent Server)

    SIP协议采用Client/Server模型.每一个请求(Request)触发服务器的一个操作:每个操作被称为方法(Method):每个请求除了指明具体方法外,还携带了一系列的头域(Header fi ...

  7. wamp配置小细节

    1. 问题:在安装后,把phpMyadmin改密码后,再次登陆会提示出错.访问被拒绝. 原因:这是因为WampServer设置了直接登陆. 解法:修改config.inc.php文件中$cfg['Se ...

  8. Ros学习——值得学习的package

    RViz是一款强大的可视化工具,它允许你查看机器人中的传感器和内部状态. TF程序包(package)提供在机器人所使用到的各种坐标系之间的变换功能,并保持跟踪这些变换的变化. actionlib - ...

  9. Luogu 2114 [NOI2014]起床困难综合症

    还挺简单的. 发现这几个二进制运算并不会进位,所以我们从高到低按位贪心,一位一位计算贡献. 发现$2^{30}$刚好大于$1e9$,所以最多只要算29位. 首先算出一个全都是$0$的二进制数和一个全都 ...

  10. Luogu 1641 [SCOI2010]生成字符串

    结果和dp没有一点关系…… 30分算法:设$f_{i, j}$表示已经选了$i$个并且有$j$个是白色的状态数,转移显然,最后答案就是$f_{n + m, m}$,时间复杂度$O(n^{2})$. 1 ...