当前位置:网站首页>Li Kou daily question - day 28 -566 Reshape matrix
Li Kou daily question - day 28 -566 Reshape matrix
2022-06-26 17:55:00 【Chongyou research Sen】
2022.6.26 Did you brush the questions today ?
subject :
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 .
analysis :
The meaning of this question is to give you a two-dimensional array , Then I'll give you another line , Column size , You need to judge the new line , Whether the column size can form a new array under the size of the original two-dimensional array , Return a new array if possible , Otherwise, the original two-dimensional array is returned , for example .
【【1,2】,【3,4】】,r=1,c=4, So you can construct 【1,2,3,4】
【【1,2】,【3,4】】,r=1,c=3 Then you can't form a new array
Ideas . For reshaping matrix types , We all need to treat the original array as a one-dimensional array , And then use it ,/ and % Two residual methods are used for assignment .
analysis :
1. Violent solution
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
vector<vector<int>>vec(r, vector<int>(c));
if (r * c != mat.size()*mat[0].size())
{
return mat;
}
else
{
for (auto i = 0; i < mat.size() * mat[0].size(); i++)
{
vec[i / c][i % c] = mat[i / mat[0].size()][i % mat[0].size()];
}
}
return vec;
}
};边栏推荐
- Using redis for user access data statistics hyperloglog and bitmap advanced data types
- Notes on flowus
- Comp281 explanation
- 一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
- [buuctf.reverse] 126-130
- 关于FlowUs这一款国民好笔记
- Data Encryption Standard DES security
- [buuctf.reverse] 126-130
- Viteconfigure project path alias
- wechat_微信小程序中解决navigator进行页面跳转并传递参数问题
猜你喜欢

RSA encryption and decryption details

关于FlowUs这一款国民好笔记

Daily record 2

二分查找法-1

Preparing for the Blue Bridge Cup and ccf-csp

Vue--vuerouter cache routing component

Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume

halcon之区域:多种区域(Region)特征(5)
![[recommendation system learning] recommendation system architecture](/img/a8/448f6e708227555bb6b32cdc652435.png)
[recommendation system learning] recommendation system architecture

Discussion and generation of digital signature and analysis of its advantages
随机推荐
MySQL add column failed because there was data before, not null by default
Secrets of gear contract
离婚协议中的几个重点
【动态规划】剑指 Offer II 091. 粉刷房子
Cloud native 02: Alibaba cloud cloud efficient flow pipeline
RSA概念详解及工具推荐大全 - lmn
Decision tree and random forest
map和filter方法对于稀缺数组的处理
二分查找-2
9、智慧交通项目(2)
Prometeus 2.34.0 新特性
Using redis for user access data statistics hyperloglog and bitmap advanced data types
DoS及攻擊方法詳解
Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
[dynamic planning] Jianzhi offer II 091 Paint the house
Plt How to keep show() not closed
同花顺开户怎么样安全吗?怎么炒股开户
QPushButton 样式使用示例(以及按钮setmenu添加下拉菜单的方法)
LeetCode——226. 翻转二叉树(BFS)
我想知道,我在肇庆,到哪里开户比较好?网上开户是否安全么?