在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 都是正数。
  1. class Solution {
  2. public:
  3. vector<vector<int> > matrixReshape(vector<vector<int> >& nums, int r, int c) {
  4. int R = nums.size();
  5. if(R == 0)
  6. return nums;
  7. int C = nums[0].size();
  8. if(r * c != R * C)
  9. return nums;
  10. vector<vector<int> >res;
  11. int cntR = 0;
  12. int cntC = 0;
  13. for(int i = 0; i < r; i++)
  14. {
  15. vector<int> temp;
  16. for(int j = 0; j < c; j++)
  17. {
  18. temp.push_back(nums[cntR][cntC]);
  19. if(cntC == C - 1)
  20. {
  21. cntC = 0;
  22. cntR++;
  23. }
  24. else
  25. {
  26. cntC++;
  27. }
  28. }
  29. res.push_back(temp);
  30. }
  31. return res;
  32. }
  33. };

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 Concurrency (35): 线程池 part 8 线程池的实现及原理 (3)[转]

    线程池任务执行结果 这一节来探讨下线程池中任务执行的结果以及如何阻塞线程.取消任务等等. 1 package info.imxylz.study.concurrency.future;2 3 publ ...

  2. Spring MVC(十三)--保存并获取属性参数

    这里的属性参数主要是指通过request.session.cookie等设置的属性,有时候我们需要将一些请求的参数保存到HTTP的request或者session对象中去,在控制器中也会进行设置和获取 ...

  3. JavaEE三层架构

    三层架构         三层架构是javaee规范中的推荐架构,传统意义上是分为表示层(UI).业务逻辑层(BLL).数据访问层(DAL).在javaee的开发中,三层架构具体分为表示层(web层) ...

  4. 05_Spring AOP原理

    理解AOP相关概念 Target(目标对象):代理的目标对象 Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. ...

  5. Free- Linux必学的60个命令

    1.作用 free命令用来显示内存的使用情况,使用权限是所有用户. 2.格式 free [-b|-k|-m] [-o] [-s delay] [-t] [-V] 3.主要参数 -b -k -m:分别以 ...

  6. mysql设置密码登录

    参考: https://blog.csdn.net/Light_Breeze/article/details/82070222 https://www.jianshu.com/p/d979df2791 ...

  7. JZOJ5898【NOIP2018模拟10.6】距离统计

    题目 题目大意 给你带边权的树,然后有多高询问,每次询问距离某个点第kkk近的节点的距离. 思考 一眼看下去,首先就是想到如何动态的区间第K大,还要支持区间修改-- 于是想了半天,觉得不可做-- 最终 ...

  8. Ionic 列表、文本 自动 换行

    1.采用row 布局的row-warp 来处理 <div class="item item-icon-right"> <span>图片相册</span ...

  9. MyBatis配置文件(一)――properties属性

    MyBatis配置文件中有很多配置项,这些配置项分别代表什么,有什么作用,需要理一下了.先通过下面这个例子来看都有哪些配置项 <?xml version="1.0" enco ...

  10. Python实例4- 列表到字典的函数,针对好玩游戏物品清单

    假设征服一条龙的战利品表示为这样的字符串列表: dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'] 写一个名为 ...