LeetCode 566. Reshape the Matrix (C++)
题目:
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:
- The height and width of the given matrix is in range [1, 100].
- 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++)的更多相关文章
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- LeetCode 566. Reshape the Matrix (重塑矩阵)
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- LeetCode 566 Reshape the Matrix 解题报告
题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
- leetcode 566 Reshape the Matrix 重塑矩阵
参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...
- LeetCode - 566. Reshape the Matrix (C++) O(n)
1. 题目大意 根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数.如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵.否则,重塑矩阵.其中两个矩阵中的数据顺序不变(先行后列). ...
- 566. Reshape the Matrix - LeetCode
Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ...
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
- [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 ...
- 【leetcode】566. Reshape the Matrix
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
随机推荐
- Kafka设计解析(二)Kafka High Availability (上)
转载自 技术世界,原文链接 Kafka设计解析(二)- Kafka High Availability (上) Kafka从0.8版本开始提供High Availability机制,从而提高了系统可用 ...
- Mysql数据库 day1
Mysql数据库属于关系型数据库(mysql.oracle.sql server),非关系型数据库有DB2.Redis MySQL执行原理,逻辑分层.更改数据库处理引擎 作者:Stanley 罗昊 [ ...
- Angular4 自制打地鼠游戏
前端工程师新手一枚,之前一直做些小设计,以及静态页面的编写工作.刚刚接触 Angular 没有多久,四个月前对于 Javascript也只是会写 alert 之流,现在进步算是很大,下面是自制的打地鼠 ...
- rem布局简介
移动端常见布局: 1.流式布局 高度固定,宽度自适应 2.响应式布局 能够用一套代码适应不同尺寸屏幕 3.rem布局 宽高自适应,能实现整个页面像一张图片一样缩放且不失真的效果. rem布局: em: ...
- Sql server 查看锁和Kill 死锁进程
死锁的概念 死锁就是两个或多个会话(SPID)相互请求对方持有的锁资源,导致循环等待的情况.下面两种方法都是用来粗暴的解决死锁的. # 已知阻塞进程ID KILL ID SELECT blocking ...
- 微信小程序的经纬度不想写死,需要转成number类型不能用浮点型
click: function (e) { var msg = this.data.placeData; var latitude = Number(msg.latitude) var longitu ...
- 18 [网络编程]-UDP
1.TCP VS UDP tcp基于链接通信 基于链接,则需要listen(backlog),指定连接池的大小 基于链接,必须先运行的服务端,然后客户端发起链接请求 对于mac系统:如果一端断开了链接 ...
- Kubernetes学习之路(三)之Mater节点二进制部署
K8S Mater节点部署 1.部署Kubernetes API服务部署 apiserver提供集群管理的REST API接口,包括认证授权.数据校验以及集群状态变更等. 只有API Server才能 ...
- Redis学习之路(一)之缓存知识体系
转自:https://www.unixhot.com/page/cache 缓存分层 缓存分级 内容 内容简介/主要技术关键词 用户层 DNS 浏览器DNS缓存 Firefox默认60秒,HTML5的 ...
- 解决公司的垃圾wifi dhcp获取不到ip 以及配上ip也不能联网的原因
用手机连公司的wifi时,发现dhcp自动获取不到ip,然后配置了静态ip,但是还是无法联网, 然后发现鸡巴垃圾公司傻逼操她妈的逼原来是google的dns导致不能用??? 换成114.114.11 ...