当前位置:网站首页>566. reshaping the matrix
566. reshaping the matrix
2022-06-12 17:40:00 【Yangpengwei】
subject : Reshaping the matrix 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 .
Ideas : It means that one mn The matrix of becomes another rc Matrix , Ask if it can be achieved . Be sure that the total number of elements in the matrix is the same , Then all the elements are reassigned , Go through all the elements , Then, according to the number of new columns, put them in the corresponding position .
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;
}
}
};
边栏推荐
- TensorFlow2训练数据集的两种方式
- R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于单个分组下的阳性指标的概率值及其95%置信区间、基于折线图、仅仅适用于目标类别为二分类)
- SqlServer常用语句及函数
- bug记录:更新数据库时报错:Data truncation: Incorrect datetime value:
- Cesium parabolic equation
- 错误记录:IllegalStateException: Optional int parameter ‘xxxx‘ is
- 字节飞书人力资源套件三面
- Introduction of one object one code tracing system
- Hangzhou AI developer meetup registration opens!
- MIPS 通用寄存器 + 指令
猜你喜欢
迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅
The significance of writing technology blog
office应用程序无法正常启动0xc0000142
Quick start sweep crawler framework
消息队列存储消息数据的 MySQL 表格
Arm64 stack backtracking
TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘
Learn the mitmproxy packet capturing tool from scratch
Notes on user experience elements: user centered product design
How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
随机推荐
AlibabaProtect.exe如何删除、卸载
Operating with idle funds
文章名字
ftrace
写技术博客的意义
Enterprise internal online training system source code
Record the use of yolov5 to detect rotating targets
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouped variables and visual
[BSP video tutorial] stm32h7 video tutorial Issue 8: the last issue of the MDK theme, the new generation of debugging technologies event recorder and RTT, and using stm32cubemx to generate project tem
消息队列实战之队列优先级
LCD参数解释及计算
Unprecedented analysis of Milvus source code architecture
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
[CSP]202012-2期末预测之最佳阈值
The R language uses the PDF function to save the visual image results to the PDF file, uses the PDF function to open the image device, uses the dev.off function to close the image device, and customiz
认识函数原创
MySQL transaction introduction and transaction isolation level
C#操作数据库增查业务数据值内容案例学校表
数据库SQL操作基础
Arm64 Stack backtrack