当前位置:网站首页>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;
}
};
边栏推荐
- MongoDB命令汇总
- 最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
- [1] ROS2基础知识-操作命令总结版
- Realbasicvsr test pictures and videos
- Split screen bug notes
- Move base parameter analysis and experience summary
- [QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
- php——laravel缓存cache
- 提升树莓派性能的方法
- Use of polarscatter function in MATLAB
猜你喜欢
566. 重塑矩阵
Getting started with MySQL
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
Digital IC Design SPI
Ways to improve the performance of raspberry pie
得物客服热线的演进之路
Vscade editor esp32 header file wavy line does not jump completely solved
1. Deep copy 2. Call apply bind 3. For of in differences
xshell连接服务器把密钥登陆改为密码登陆
数字ic设计——SPI
随机推荐
Thread pool reject policy best practices
What parameters need to be reconfigured to replace the new radar of ROS robot
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
Scrapy教程经典实战【新概念英语】
MySQL error 28 and solution
Split screen bug notes
高端了8年,雅迪如今怎么样?
Distributed transaction solution
118. 杨辉三角
PAcP learning note 1: programming with pcap
Redis只能做缓存?太out了!
分屏bug 小记
OSI 七层模型
Navicat运行sql文件导入数据不全或导入失败
[learning notes] zkw segment tree
Mongodb meets spark (for integration)
648. 单词替换 : 字典树的经典运用
记一次 .NET 某新能源系统 线程疯涨 分析
JS function 返回多个值
ESP32 ① 编译环境