当前位置:网站首页>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;
}
};
边栏推荐
- Navicat run SQL file import data incomplete or import failed
- How to make the new window opened by electorn on the window taskbar
- MongoDB的导入导出、备份恢复总结
- 2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
- QQ的药,腾讯的票
- Why can basic data types call methods in JS
- 2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
- Ikvm of toolbox Net project new progress
- [etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
- MongoDB内部的存储原理
猜你喜欢
高端了8年,雅迪如今怎么样?
QQ的药,腾讯的票
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
LED light of single chip microcomputer learning notes
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
OSI 七层模型
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
cmake 学习使用笔记(一)
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
JS缓动动画原理教学(超细节)
随机推荐
Error lnk2019: unresolved external symbol
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
Esp32 construction engineering add components
Signal strength (RSSI) knowledge sorting
postgresql array类型,每一项拼接
User management summary of mongodb
JS缓动动画原理教学(超细节)
[learning notes] zkw segment tree
error LNK2019: 无法解析的外部符号
118. 杨辉三角
cmake 学习使用笔记(一)
LED light of single chip microcomputer learning notes
ESP32系列专栏
Final review notes of single chip microcomputer principle
Mongodb replication (replica set) summary
Cmake learning and use notes (1)
.net core 关于redis的pipeline以及事务
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
PAcP learning note 3: pcap method description