当前位置:网站首页>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;
}
};
边栏推荐
- Realize the IP address home display function and number home query
- [etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
- 数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
- Shell batch file name (excluding extension) lowercase to uppercase
- RealBasicVSR测试图片、视频
- Show the mathematical formula in El table
- clion mingw64中文乱码
- postgresql array类型,每一项拼接
- Signal strength (RSSI) knowledge sorting
- OSI 七层模型
猜你喜欢
Error lnk2019: unresolved external symbol
分布式事务解决方案
Cinnamon taskbar speed
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
《厌女:日本的女性嫌恶》摘录
Introduce six open source protocols in detail (instructions for programmers)
LIS 最长上升子序列问题(动态规划、贪心+二分)
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Digital IC Design SPI
Show the mathematical formula in El table
随机推荐
Write it down once Net a new energy system thread surge analysis
Move base parameter analysis and experience summary
MongoDB复制(副本集)总结
[learning notes] segment tree selection
MongoDB优化的几点原则
mysql 局域网内访问不到的问题
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
Cinnamon 任务栏网速
Split screen bug notes
Talk about pseudo sharing
Mongodb meets spark (for integration)
Read PG in data warehouse in one article_ stat
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
干货|总结那些漏洞工具的联动使用
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
靠卖概念上市,认养一头牛能走多远?
flask session伪造之hctf admin
MongoDB的用户管理总结