当前位置:网站首页>566. 重塑矩阵
566. 重塑矩阵
2022-07-07 11:38:00 【yitahutu79】
在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据。
给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。
重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。
如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
示例 1:
输入:mat = [[1,2],[3,4]], r = 1, c = 4
输出:[[1,2,3,4]]
示例 2:
输入:mat = [[1,2],[3,4]], r = 2, c = 4
输出:[[1,2],[3,4]]
提示:
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 slice summary
- DID登陆-MetaMask
- [learning notes] segment tree selection
- Introduction and basic use of stored procedures
- 实现IP地址归属地显示功能、号码归属地查询
- mysql 局域网内访问不到的问题
- Data refresh of recyclerview
- 2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
- 作战图鉴:12大场景详述容器安全建设要求
- Move base parameter analysis and experience summary
猜你喜欢
Storage principle inside mongodb
Cinnamon 任务栏网速
如何让join跑得更快?
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
How far can it go to adopt a cow by selling the concept to the market?
Error lnk2019: unresolved external symbol
LED light of single chip microcomputer learning notes
MySQL error 28 and solution
Distributed transaction solution
单片机原理期末复习笔记
随机推荐
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
C语言数组相关问题深度理解
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
Read PG in data warehouse in one article_ stat
LED light of single chip microcomputer learning notes
Cinnamon 任务栏网速
ESP32系列专栏
Use of polarscatter function in MATLAB
Clion mingw64 Chinese garbled code
Scripy tutorial classic practice [New Concept English]
OSI 七层模型
10 张图打开 CPU 缓存一致性的大门
Milkdown 控件图标
[learning notes] zkw segment tree
JS function 返回多个值
[learning notes] segment tree selection
单片机原理期末复习笔记
Mongodb command summary
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
Vscode编辑器ESP32头文件波浪线不跳转彻底解决