当前位置:网站首页>566. 重塑矩阵
566. 重塑矩阵
2022-06-12 17:32:00 【杨鹏伟】
题目:重塑矩阵 在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据。 给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。 重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。 如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
思路:意思就是把一个mn的矩阵变成另一个rc的矩阵,问是否能实现。能肯定矩阵内元素总数一样,然后的话就是所有元素重新赋值,遍历一遍所有元素,然后根据新的列的数量把其放到对应的位置即可。
code
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
int hang = mat.size();
int lie = mat[0].size();
if(hang * lie != r * c) return mat;
else{
vector<vector<int>> ans(r,vector<int>(c));
for(int i=0;i<hang*lie;i++){
ans[i/c][i%c] = mat[i/lie][i%lie];
}
return ans;
}
}
};边栏推荐
- Su directly switches to super administrator mode, so that many error reports can be avoided
- R语言计算data.table在一个分组变量的值固定的情况下另外一个分组变量下指定数值变量的均值
- Gerrit+2 triggers Jenkins task
- How to use the official documents of pytorch and torchvision
- Detailed explanation of shardingjdbc database and table
- selenium元素定位
- R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
- How to view, modify, and delete SSH
- (4) Golang operator
- How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
猜你喜欢

Modify the configuration of the router connected to your computer. The website is 192.168.1.1

Arm64 stack backtracking

rolabelImg的安装使用

Learn the mitmproxy packet capturing tool from scratch

Gerrit触发Jenkins SonarQube扫描

MySQL事务简介、事务隔离级别

Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现

Evolution and thinking of Taobao native R & D mode | DX R & D mode

如何查看、修改和删除SSH

《用户体验要素:以用户为中心的产品设计》笔记
随机推荐
TensorFlow2训练数据集的两种方式
Making nearly $90billion, Buffett's latest heavy stock exposure
Fiddler抓包几种常用功能介绍(停止抓包、清空会话窗内容、过滤请求、解码、设置断点......)
Arm64棧回溯
R语言使用pdf函数将可视化图像结果保存到pdf文件中、使用pdf函数打开图像设备、使用dev.off函数关闭图像设备、自定义width参数和height参数指定图像的宽度和高度
Implementation of asynchronous query of Flink dimension table and troubleshooting
5-5配置Mysql复制 基于日志点的复制
山东大学软件学院项目实训-创新实训-山大软院网络攻防靶场实验平台(二十五)-项目个人总结
2080 virtual machine login command
Interesting LD_ PRELOAD
Arm64 Stack backtrack
Feedback compilation
The R language uses the aggregate The plot function visualizes the summary statistical information of each subset (visualization is based on the probability value and its 95% confidence interval of th
记录使用yolov5进行旋转目标的检测
R language calculates data Table specifies the mean value of a numeric variable when the value of one grouped variable is fixed and another grouped variable
32-bit MCU mm32f0040 with smart micro high performance M0 kernel
Introduction of one object one code tracing system
The R language uses the plot function to visualize the data scatter chart, and uses font The axis parameter specifies that the font type of the axis scale label is italic
String s = null ; String s = new String();String s =““ ;String s ;有什么区别?
一物一码追踪溯源系统介绍