当前位置:网站首页>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;
}
};
边栏推荐
- Vscade editor esp32 header file wavy line does not jump completely solved
- Solve the cache breakdown problem
- centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
- PAcP learning note 3: pcap method description
- Ikvm of toolbox Net project new progress
- MongoDB的导入导出、备份恢复总结
- Esp32 ① compilation environment
- MongoDB的用户管理总结
- Final review notes of single chip microcomputer principle
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
猜你喜欢

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?

如何让join跑得更快?

MySQL入门尝鲜

Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set

Cinnamon taskbar speed

cmake 学习使用笔记(一)

实现IP地址归属地显示功能、号码归属地查询

Navicat运行sql文件导入数据不全或导入失败

Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
随机推荐
OSI 七层模型
xshell连接服务器把密钥登陆改为密码登陆
Data refresh of recyclerview
Shell batch file name (excluding extension) lowercase to uppercase
Solve the cache breakdown problem
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Custom thread pool rejection policy
简单好用的代码规范
Realize the IP address home display function and number home query
Introduce six open source protocols in detail (instructions for programmers)
MATLAB中polarscatter函数使用
LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
LeetCode简单题分享(20)
QQ的药,腾讯的票
单片机原理期末复习笔记
MongoDB的导入导出、备份恢复总结
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
C语言数组相关问题深度理解
华为镜像地址
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images