在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。

给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的行数和列数。

重构后的矩阵需要将原始矩阵的所有元素以相同的行遍历顺序填充。

如果具有给定参数的reshape操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。

示例 1:

输入: nums = [[1,2], [3,4]] r = 1, c = 4 输出: [[1,2,3,4]] 解释: 行遍历nums的结果是 [1,2,3,4]。新的矩阵是 1 * 4 矩阵, 用之前的元素值一行一行填充新矩阵。

示例 2:

输入: nums = [[1,2], [3,4]] r = 2, c = 4 输出: [[1,2], [3,4]] 解释: 没有办法将 2 * 2 矩阵转化为 2 * 4 矩阵。 所以输出原矩阵。

注意:

  1. 给定矩阵的宽和高范围在 [1, 100]。
  2. 给定的 r 和 c 都是正数。
class Solution {
public:
vector<vector<int> > matrixReshape(vector<vector<int> >& nums, int r, int c) {
int R = nums.size();
if(R == 0)
return nums;
int C = nums[0].size();
if(r * c != R * C)
return nums;
vector<vector<int> >res;
int cntR = 0;
int cntC = 0;
for(int i = 0; i < r; i++)
{
vector<int> temp;
for(int j = 0; j < c; j++)
{
temp.push_back(nums[cntR][cntC]);
if(cntC == C - 1)
{
cntC = 0;
cntR++;
}
else
{
cntC++;
}
}
res.push_back(temp);
}
return res;
}
};

Leetcode566.Reshape the Matrix重塑矩阵的更多相关文章

  1. [LeetCode] 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 重塑矩阵

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

  3. leetcode566. Reshape the Matrix

    https://leetcode.com/problems/reshape-the-matrix/description/ public int[][] matrixReshape(int[][] n ...

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

    566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...

  5. [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix

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

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

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

  7. C#LeetCode刷题之#566-重塑矩阵( Reshape the Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3720 访问. 在MATLAB中,有一个非常有用的函数 resha ...

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

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

  9. 566. Reshape the Matrix矩阵重排

    [抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...

随机推荐

  1. Java-Shiro:目录

    ylbtech-Java-Shiro:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://yl ...

  2. Docker系列(十):Kubernetes集群入门

    kubenetes安装 官网:https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/getting-started- guide ...

  3. ymfx

    一.APIView 入口 在路由层执行as_view()方法 rest-framework/views.py/class APIView/def as_view() 可以看到,APIView继承了Dj ...

  4. 【neo4j】neo4j Desktop1.1.9,windows 安装

    一.neo4j 介绍 Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储 ...

  5. 查看MySql版本号命令

    转自:https://blog.csdn.net/qq_38486203/article/details/80324014​ 这里介绍四中不同的方法,它们分别运行在不同的环境中,最后对每种方法的优劣以 ...

  6. 使用 MongoDB shell访问MongoDB

  7. JZOJ5967 常数国

    题目 像素有点低啊~ 算了凑合一下就好啦~ 题目大意 给你一个首尾相接的数列,每次对一个区间进行操作: 顺时针操作,如果当前值比vvv大,就交换.输出最后的vvv. 比赛思路 首先这题的时限这么仁慈, ...

  8. HZOI20190828模拟32题解

    题面:https://www.cnblogs.com/Juve/articles/11428730.html chinese: 考虑$\sum\limits_{i=0}^{n*m}i*f_i$的意义: ...

  9. CF 981C Useful Decomposition

    题面 题目大意 给定一棵树,要求划分出几条链,使这几条链交于一点. 解题思路 因为所有链都要交于一点,所以必须交于一个度数最多的点.这样就形成了一个菊花形.然后从这个点出发到它的子树,判断子树的度数是 ...

  10. php循环语句(二)

    for 循环 for 循环用于您预先知道脚本需要运行的次数的情况. 语法 for (初始值; 条件; 增量) { 要执行的代码; } 参数: 初始值:主要是初始化一个变量值,用于设置一个计数器(但可以 ...