[Array] 566. Reshape the Matrix
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 andcolumn 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.
思路:本题的意思是将一个m*n维数组转换成l*c维,难点在于遍历嵌套vector元素、求vector的大小、初始化嵌套vector、按照数组行列输出等。
代码:
vector<vector<int>>matrixreshape(vector<vector<int>& nums, int r, int c>){
size_t m = nums.size;//vector外层嵌套,即为行
size_t n = nums[].size;//vector内层嵌套,即为列
if(r * c != m * n)
return nums;
else{
int k =;
vector<vector<int>>tmp(r, vector<int>(c, ));//将tmp初始化为r行c列的全零二维数组
for(size_t i = ; i < m; i++){
for(size_t j = ; j < n; j++){
tmp[k / c][k % c] = nums[i][j];//按照c列输出,不用担心r行,因为c*r=k
k++;
}
}
return tmp;
}
}
vector初始化:
/vector<T> v(n,i)形式,v包含n 个值为 i 的元素
vector<int> ivec(10,0);
//vector<T> v(v1)形式,v是v1 的一个副本
vector<int> ivec1(ivec);
//vector<T> v(n)形式,v包含n 个值初始化的元素
vector<int> ivec2(10);
下面是Mat中的reshape
摘自:http://blog.csdn.net/monologue_/article/details/8659632
只是在逻辑上改变矩阵的行列数或者通道数,没有任何的数据的复制,也不会增减任何数据,因此这是一个O(1)的操作,它要求矩阵是连续的。
C++: Mat Mat::reshape(int cn, int rows=0 const)
cn:目标通道数,如果是0则保持和原通道数一致;
rows:目标行数,同上是0则保持不变;
改变后的矩阵要满足 rows*cols*channels 跟原数组相等,所以如果原来矩阵是单通道3*3的,调用Reshape(0,2)是会报错的,因为3*3*1不能被2*1整除。
应用:在提取特征时,往往需要把特征矩阵变成一个行向量
[Array] 566. Reshape the Matrix的更多相关文章
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- 566. Reshape the Matrix - LeetCode
Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ...
- 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&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 (C++)
题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...
- 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
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
随机推荐
- (转)行为树(Behavior Tree)实践(1)– 基本概念
通过一个例子来介绍一下行为树的基本概念,会比较容易理解,看下图: 这是我们为一个士兵定义的一颗行为树(可以先不管这些绿圈和红圈是干吗的),首先,可以看到这是一个树形结构的图,有根节点,有分支,而且子节 ...
- python3 selenium 超时停止加载,并且捕捉异常, 进行下一步【亲测有效】
from selenium import webdriver import os import re class GetPage: def __init__(self, url_path): self ...
- IPMI 远程配置
#重启ipmi服务 #重启ipmi服务 #将 channel 1 设置为静态 IP #设置 IP #设置 channel 1 掩码 #设置 channel 1 网关 #查看用户名及 ID #设置ID号 ...
- ST(RMQ)算法(在线)求LCA
在此之前,我写过另一篇博客,是倍增(在线)求LCA.有兴趣的同学可以去看一看.概念以及各种暴力就不在这里说了,那篇博客已经有介绍了. 不会ST算法的同学点这里 ST(RMQ)算法在线求LCA 这个算法 ...
- opencv-Mat数据类型及位数总结
转自:http://blog.sina.com.cn/s/blog_662c7859010105za.html 在OpenCV里面,许多数据结构为了达到內存使用的最优化,通常都会用它最小上限的空间来分 ...
- 运行pip报错:Fatal error in launcher: Unable to create process using '’路径’'
参考此文:https://blog.csdn.net/cjeric/article/details/73518782 win7笔记本安装了Python2.7,Python3.7,以及anaconda3 ...
- js 实现页面局部(或图片)放大功能(vue)
方法: adjustStart1 (e) { e.preventDefault() let event = e.touches if (event.length === 2) { this.style ...
- CF #578 Div2
// 比赛链接:https://codeforces.com/contest/1200 A - Hotelier 题意: 有一家旅馆有10间房,编号0~9,从左到右顺序排列.旅馆有左右两扇门,每次新来 ...
- Python使用微信接入图灵机器人
1.wxpy库介绍 wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展. 文档地址:https://wxpy.readthedocs.io 从 PYPI 官 ...
- Django项目: 项目环境搭建 ---- 三、在码云平台创建项目&推送到码云上
三.在码云平台创建项目 git服务平台: 主要使用github(最主流) 国内访问速度慢 托管私有项目收费 国内一般使用码云gitee 国内访问速度快 托管私有项目免费(限制开发人数) 公司中使用gi ...