当前位置:网站首页>566. Reshaping the matrix
566. Reshaping the matrix
2022-07-07 13:37:00 【yitahutu79】
stay MATLAB in , There's a very useful function reshape , It can put a m x n The matrix is remodeled to another different size (r x c) The new matrix of , But keep its original data .
Give you a two-dimensional array mat It means m x n matrix , And two positive integers r and c , Represent the number of rows and columns of the matrix to be reconstructed respectively .
The reconstructed matrix needs to replace all elements of the original matrix with the same Row traversal order fill .
If the reshape The operation is feasible and reasonable , The new reshaping matrix is output ; otherwise , Output raw matrix .
Example 1:

Input :mat = [[1,2],[3,4]], r = 1, c = 4
Output :[[1,2,3,4]]
Example 2:
Input :mat = [[1,2],[3,4]], r = 2, c = 4
Output :[[1,2],[3,4]]
Tips :
m == mat.length
n == mat[i].length
1 <= m, n <= 100
-1000 <= mat[i][j] <= 1000
1 <= r, c <= 300
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
int m = mat.size();
int n = mat[0].size();
if (m * n != r * c) {
return mat;
}
vector<vector<int>> arr(r, vector<int>(c));
for (int i = 0; i < m * n; i++) {
arr[i/c][i%c] = mat[i/n][i%n];
}
return arr;
}
};
边栏推荐
- QQ medicine, Tencent ticket
- Deep understanding of array related problems in C language
- RealBasicVSR测试图片、视频
- 2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
- MongoDB复制(副本集)总结
- 华为镜像地址
- Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
- Mongodb replication (replica set) summary
- 2022-7-6 Leetcode 977.有序数组的平方
- 实现IP地址归属地显示功能、号码归属地查询
猜你喜欢
随机推荐
.net core 关于redis的pipeline以及事务
Navicat run SQL file import data incomplete or import failed
PHP - laravel cache
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
2022-7-6 Leetcode 977.有序数组的平方
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
error LNK2019: 无法解析的外部符号
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
Error lnk2019: unresolved external symbol
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
[Presto profile series] timeline use
Final review notes of single chip microcomputer principle
Toraw and markraw
566. 重塑矩阵
Flink | 多流转换
得物客服热线的演进之路
QQ medicine, Tencent ticket
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
MongoDB复制(副本集)总结








