当前位置:网站首页>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运行sql文件导入数据不全或导入失败
How far can it go to adopt a cow by selling the concept to the market?
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
高端了8年,雅迪如今怎么样?
LED light of single chip microcomputer learning notes
Write it down once Net a new energy system thread surge analysis
118. 杨辉三角
Xshell connection server changes key login to password login
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
Cinnamon Applet 入门
随机推荐
MongoDB命令汇总
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
Redis只能做缓存?太out了!
ESP32 ① 编译环境
MongoDB内部的存储原理
Write it down once Net a new energy system thread surge analysis
Read PG in data warehouse in one article_ stat
华为镜像地址
Ikvm of toolbox Net project new progress
Scrapy教程经典实战【新概念英语】
How far can it go to adopt a cow by selling the concept to the market?
Getting started with cinnamon applet
Thread pool reject policy best practices
DID登陆-MetaMask
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
Mongodb meets spark (for integration)
QQ的药,腾讯的票
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
QQ medicine, Tencent ticket
RealBasicVSR测试图片、视频