当前位置:网站首页>力扣每日一题-第28天-566.重塑矩阵
力扣每日一题-第28天-566.重塑矩阵
2022-06-26 17:24:00 【重邮研究森】
2022.6.26今天你刷题了吗?
题目:
在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据。
给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。
重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。
如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
分析:
本题的意思是给你一个二维数组,然后再给你一个行,列大小,你需要判断新的行,列大小在原二维数组大小下能不能形成新的数组,如果可以则返回新数组,否则返回原二维数组,例如。
【【1,2】,【3,4】】,r=1,c=4,那么可以构成【1,2,3,4】
【【1,2】,【3,4】】,r=1,c=3那么不可以构成新数组
思路。对于重塑矩阵类型,我们都需要先把原数组看成一维数组,然后利用,/ 和 %两种取余方式进行赋值。
解析:
1.暴力求解
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;
}
};边栏推荐
- How about opening an account at Guojin securities? Is it safe?
- Cloud native 02: Alibaba cloud cloud efficient flow pipeline
- 【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
- Platform management background and merchant menu resource management: Design of platform management background data service
- The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
- ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
- ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
- Niuke network: Design LRU cache structure design LFU cache structure
- 链游系统开发技术方案设计丨NFT链游系统开发流程及源码
- Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
猜你喜欢
随机推荐
在国金证券开户怎么样?保障安全吗?
直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
Incomplete line spacing adjustment of formula display in word
【万字总结】以终为始,详细分析高考志愿该怎么填
sparksql如何通过日期返回具体周几-dayofweek函数
离婚协议中的几个重点
Implementation of MySQL master-slave architecture
Teach you to learn dapr - 2 Must know concept
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
玩轉Linux,輕松安裝配置MySQL
Microservice architecture practice: user login and account switching design, order query design of the mall
Use middleware to record slow laravel requests
Introduction to minimal API
Fire evacuation and self rescue... This safety production and fire training is full!
物联网协议的王者:MQTT
Halcon's region: features of multiple regions (5)
Vue--vuerouter cache routing component
mysql Add column 失败 因为之前有数据,不是默认null 不行
玩转Linux,轻松安装配置MySQL
In those years, interview the abused red and black trees








![[C language] static modifies local variables](/img/bf/9084d2e924c3e1e244568562a83d74.jpg)