当前位置:网站首页>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;
}
}
};边栏推荐
- 写技术博客的意义
- (6) Control statement if/else switch
- 32-bit MCU mm32f0040 with smart micro high performance M0 kernel
- channel原创
- (4) Golang operator
- MIPS 通用寄存器 + 指令
- 内核中断整体流程图
- How to change Golan back to the English version when it becomes the Chinese version
- I heard that distributed IDS cannot be incremented globally?
- Arm64 Stack backtrack
猜你喜欢

Cesium抛物线方程

Compilation optimization of performance optimization

进阶之大山-asp.net core 路由程序基础使用演示0.1
Memory control of node

C#操作数据库增查业务数据值内容案例学校表

Yyds dry goods inventory leetcode question set 911 - 920

Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134) vulnerability recurrence

迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅

Fiddler抓包几种常用功能介绍(停止抓包、清空会话窗内容、过滤请求、解码、设置断点......)

How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
随机推荐
Is Huishang futures company reliable in opening accounts and safe in trading?
32-bit MCU mm32f0040 with smart micro high performance M0 kernel
Volcano engine held a video cloud technology force summit and released a new experience oriented video cloud product matrix
Hangzhou AI developer meetup registration opens!
grpc-swift入门
字节飞书人力资源套件三面
(4) Golang operator
Understanding of binary search
龙芯处理器内核中断讲解
Fiddler抓包几种常用功能介绍(停止抓包、清空会话窗内容、过滤请求、解码、设置断点......)
ShardingJDBC 分库分表详解
Unprecedented analysis of Milvus source code architecture
WinForm, crystal report making
Selenium element positioning
ftrace
Project training of Software College of Shandong University - Innovation Training - network attack and defense shooting range experimental platform of Software College of Shandong University (XXV) - p
Figma from getting started to giving up
Use the official go Library of mongodb to operate the original mongodb
论文《Deep Interest Evolution Network for Click-Through Rate Prediction》
Implementation of asynchronous query of Flink dimension table and troubleshooting