当前位置:网站首页>LeetCode 566、重塑矩阵
LeetCode 566、重塑矩阵
2022-04-21 15:31:00 【亡于灬】
566、重塑矩阵
1)题目描述
在 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.lengthn == mat[i].length1 <= m, n <= 100-1000 <= mat[i][j] <= 10001 <= r, c <= 300
2)分析
直接重新赋值。
3)C++代码
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(r*c!=m*n)
return mat;
vector<vector<int>> res(r,vector<int>(c));
for(int i=0;i<m*n;i++){
res[i/c][i%c]=mat[i/n][i%n];
}
return res;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124315966
边栏推荐
- Is Baidu cloud disk easy to use or Alibaba cloud disk easy to use?
- How should businesses establish private domain traffic?
- Installation and uninstallation of MySQL
- Mysql
- 107页企业数字化转型规划设计
- 机器学习方法创建可学习的化学语法,构建可合成的单体和聚合物
- Hand in hand to teach you to realize hand-painted style graphics
- 105 page digital twin city information model CIM platform construction technical scheme
- MySQL8. 0 correct password change posture
- 外贸公司一般用什么邮箱,电子邮件如何群发?
猜你喜欢

Reading breaks ten thousand "volumes": National Reading insight 2022

企业邮箱如何申请?如何用手机号注册邮箱?

LeetCode刷题之652寻找重复的子树

107页企业数字化转型规划设计

OpenHarmony3. 1 H264 video playback Road

客户端邮件同步到webmail如何操作,电子邮件地址怎么注册?

使用konvajs三步实现一个小球游戏

终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享

Red team technology - parent process camouflage (mitre att & CK framework: t1134)

Glass: GNN tagging technique for subgraph representation learning
随机推荐
abaqus 根据坐标施加载荷- Analytical Field 载荷映射
What conditions need to be met for an app to go online in the app store?
红队技术-父进程伪装(MITRE ATT&CK框架:T1134)
Betterscroll source code, reading and learning typescript
GLASS:用于子图表示学习的 GNN 标签技巧
Take it easy, just talk about the soft test
SQL服务器如何设置起始日期查询语句
Elemetn form control --- automatically locate the position of the field when it is submitted without passing the verification field
【时序】Reformer:局部敏感哈希(LSH)实现高效 Transformer 论文笔记
107页企业数字化转型规划设计
Universal navigation: a concise and practical comprehensive navigation website
智慧工地综合解决方案
AcWing 1854. 晋升计数(模拟)
什么是电子邮件的格式?企业邮箱地址是什么?
On the import and export of browser bookmarks
Applet introduction and development tools
Latex common formula query
111頁精細化工股份公司數據字轉型解决方案
105页数字孪生城市信息模型CIM平台建设技术方案
OpenHarmony3. 1 H264 video playback Road