当前位置:网站首页>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;
}
};
边栏推荐
- 信号强度(RSSI)知识整理
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
- 【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
- Navicat运行sql文件导入数据不全或导入失败
- .net core 关于redis的pipeline以及事务
- 存储过程的介绍与基本使用
- [dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
- 如何让join跑得更快?
- Ways to improve the performance of raspberry pie
- LeetCode简单题分享(20)
猜你喜欢

Cmake learning and use notes (1)

2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天

LIS 最长上升子序列问题(动态规划、贪心+二分)

Realize the IP address home display function and number home query

2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来

【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...

DID登陆-MetaMask

Esp32 ① compilation environment
![[learning notes] agc010](/img/2c/37f2537a4dadd84adacf3da5f1327a.png)
[learning notes] agc010

靠卖概念上市,认养一头牛能走多远?
随机推荐
【日常训练--腾讯精选50】231. 2 的幂
单片机原理期末复习笔记
MongoDB 分片总结
Flink | multi stream conversion
Ogre introduction
Digital IC Design SPI
《厌女:日本的女性嫌恶》摘录
PAcP learning note 3: pcap method description
648. 单词替换 : 字典树的经典运用
PAcP learning note 1: programming with pcap
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
得物客服热线的演进之路
室内ROS机器人导航调试记录(膨胀半径的选取经验)
Esp32 construction engineering add components
Xshell connection server changes key login to password login
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
Getting started with cinnamon applet
2022-7-6 Leetcode 977.有序数组的平方
Problems that cannot be accessed in MySQL LAN
Mongodb slice summary