题目:

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:

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

分析:

给定一个矩阵,根据参数返回一个新的矩阵。

可以发现,只有给的r * c和原来矩阵行列乘积相同才可以将矩阵正确的变形,否则返回原来的矩阵。用f来确定原矩阵中元素的索引。直接两重循环将元素填进新矩阵中。

程序:

class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int max = nums.size() * nums[].size();
if(r == nums.size() || c == nums[].size() || max != r * c) return nums;
vector<vector<int>> res(r, vector<int>(c, ));
int f = ;
for(int i = ; i < r; ++i)
for(int j = ; j < c; ++j){
res[i][j] = nums[f/nums[].size()][f%nums[].size()];
f++;
}
return res;
}
};

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

  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 重塑矩阵

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

  5. LeetCode - 566. Reshape the Matrix (C++) O(n)

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

  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. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 140120 ...

  2. Property Injection in Asp.Net Core (转载)

    问: I am trying to port an asp.net application to asp.net core. I have property injection (using ninj ...

  3. 腾讯云centos7.2安装mysql5.7

    一.查看是否安装mysql rpm -qa | grep mysql 什么都没显示,说明没有安装 二.进入到opt目录下,使用wget下载官方yum源的rpm包 cd /opt wget https: ...

  4. vue指令:v-model绑定表单控件;v-model与v-bind结合使用

    一.v-model绑定表单控件 v-model 双向数据绑定:一般用于表单元素,会忽略表单元素的value.checked.selected的初始值,且将Vue实例的数据作为数据来源. 1. 单行文本 ...

  5. 如何方便的结果ajax使用html5的新type类型

    今天需要做手机端的输入表单自动生成器,突然就想到了手机端对input的输入类型支持还不错,于是翻遍了资料,有了下面的使用方法,闲话少说,上正文: html5现在可以用的新input type类型一共有 ...

  6. day 94 Django学习之django自带的contentType表

    Django学习之django自带的contentType表   通过django的contentType表来搞定一个表里面有多个外键的简单处理: 摘自:https://blog.csdn.net/a ...

  7. Go压缩文件

    Go压缩文件 首先是恭喜IG获得S8全球总决赛冠军,IG牛逼.但咱是一介草民,狂欢后,还是得老老实实的开始敲代码.最近做了一个给底层固件压缩加密的工具,是使用C#做的,已经提交出去可以正常使用的.既然 ...

  8. 20155214 2016-2017-2 《Java程序设计》第10周学习总结

    学号 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 掌握Java Socket编程 理解混合密码系统 掌握Java 密码技术相关API的使用 教材学习中的 ...

  9. check the manual that corresponds to your MySQL server version for the right syntax to use near

    一.问题 mysql插入数据时报错 sql如下 insert into t_sysconfig (servercode,key,value,remark,updatetime) values (&qu ...

  10. js 删除url指定参数

    /** * 删除当前url中指定参数 * @param names 数组或字符串 * @returns {string} */ function funcUrlDel(names) { if(type ...