1. 题目大意

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

2. 思路

由于矩阵中数据顺序不变,因此我们考虑按顺序做。原矩阵中的第i行第j列(从0开始)的数据可以记为第k个数,其中k=i*(原矩阵中的列数)+j。对应的是新产生的矩阵中的第k/c行,k%c列的元素。一一赋值。这题的关键是要小心数组的边界,正确找到对应的位置。

3. 代码

class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int total = r*c, originr = nums.size(), originc = nums[0].size();
if(total != (originr*originc)) return nums;
vector<vector <int> > res(r ,vector<int>(c));
for(int k = 0; k < total; k++) res[k / c][k % c] = nums[k / originc][k % originc];
return res;
}
};

  

LeetCode - 566. Reshape the Matrix (C++) O(n)的更多相关文章

  1. Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)

    Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...

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

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

  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++)

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

  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&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 ...

  9. 【leetcode】566. Reshape the Matrix

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

随机推荐

  1. 技巧:Vimdiff 使用(改)

    技巧:Vimdiff 使用(改) 各种 IDE 大行其道的同时,传统的命令行工具以其短小精悍,随手可得的特点仍有很大的生存空间,这篇短文介绍了一个文本比较和合并的小工具:vimdiff.希望能对在 U ...

  2. Vue填坑(1)----通过vue-cli,认识vue-router

    开始 首先,确保之前已经安装过 npm 和 nodejs(为了避免版本的问题,最好使用较新的版本). 全局安装 vue-cli : npm install -g vue-cli 新建文件夹 my-pr ...

  3. vue项目出现空格警告的原因及其解决办法

    原因: 因为你的Webpack 配置中大概是使用了 eslint-loader,这是用来规范代码风格的,在多人协作或大项目中推荐使用,不想要则可以在 webpack.config.js 中去掉.esl ...

  4. thinkphp5实现定位功能

    一.所需资源链接:百度网盘.主要包含一个ip地址库和一个ip类文件. 二.下载好后,在extend目录下面创建一个location的目录,将下载的文件解压到该目录.给类文件增加一个命名空间,便于我们使 ...

  5. SHOPEX快递物流单号查询插件

    本SHOPEX快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...

  6. Git安装配置(Linux)

    使用yum安装Git yum install git -y 编译安装 # 安装依赖关系 yum install curl-devel expat-devel gettext-devel openssl ...

  7. 20154327 Exp5 MSF基础应用

    基础问题回答 用自己的话解释什么是exploit,payload,encode. exploit漏洞利用,一般出现漏洞后,根据一些大佬们给出的POC尝试去进行漏洞利用. payload攻击负载,是我们 ...

  8. 【转载】四元数-Quaterion

    原文:四元数-Quaterion 四元数(Quaterion)   罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循“署名-非商业用途-保持一致”创作公用协议   ...

  9. Java:泛型擦除

    https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

  10. Entity Framework Core 导航属性 加载数据

    Loading Related Data https://docs.microsoft.com/en-us/ef/core/querying/related-data Eager loading me ...