题目:

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 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48380   Accepted: 18252 ...

  2. MVC下载文档

    public FileStreamResult DownFile(string content,string name) { ProInterface.ISubject ems = new ProSe ...

  3. Entity Framework Code First 遭遇主键自动生成问题

    4.0后就没有去跟踪后面的版本了.现在直接开始用5.0没想到在做User的GURD时就遭遇insert不进数据问题. ISet<User>.Add(user);_context.SaveC ...

  4. Asp.Net写入读取Xml(处理文件权限)

    1,网上关于读取写入Xml的博客比较多,参考了发现提到Xml文件权限的博客比较少.因为在开发中我发现,如果文件存于一些没有权限的路径,代码是访问不到该文件,页面会报错提示403,Forbidden.意 ...

  5. TarsGo新版本发布,支持protobuf,zipkin和自定义插件

    本文作者:陈明杰(sandyskies) Tars是腾讯从2008年到今天一直在使用的后台逻辑层的统一应用框架,目前支持C++,Java,PHP,Nodejs,Golang语言.该框架为用户提供了涉及 ...

  6. Mongodb基础知识笔记

    MongoDB介绍 MongoDB是一个基于分布式文件存储的开源文档数据库.由C++语言编写.旨在为WEB应用提供高性能.高可用性和高伸缩数据存储解决方案. MongoDB优点 MongoDB使用场景 ...

  7. Redis全方位详解--数据类型使用场景和redis分布式锁的正确姿势

    一.Redis数据类型 1.string string是Redis的最基本数据类型,一个key对应一个value,每个value最大可存储512M.string一半用来存图片或者序列化的数据. 2.h ...

  8. Flume的一些报错问题解决(持续更新中)

    严谨转载--否则追究法律责任 作者----王加鸿                                                   ----------bug 1---------- ...

  9. Golint的简易使用方法

    根据作者的说法: Golint is a linter for Go source code. Golint differs from gofmt. Gofmt reformats Go source ...

  10. lua函数定义

    FuncState proto结构数组保存函数原型信息;prev保存父函数体指针:actvar保存定义的局部变量:upvalues保存upvalue Lua源码中,专门有一个结构体FuncState用 ...