当前位置:网站首页>【 LeetCode 】 566. Reshape the matrix
【 LeetCode 】 566. Reshape the matrix
2022-07-29 15:02:00 【Crispy~】
题目
在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据.
给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数.重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充. 如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵.
示例 1:
输入:mat = [[1,2],[3,4]], r = 1, c = 4
输出:[[1,2,3,4]]
示例 2:
输入:mat = [[1,2],[3,4]], r = 2, c = 4
输出:[[1,2],[3,4]]
提示:
m == mat.length
n == mat[i].length
1 <= m, n <= 100
-1000 <= mat[i][j] <= 1000
1 <= r, c <= 300
题解
forCyclic Violent Transformation
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
//获取初始化参数
int m = mat.size();
int n = mat[0].size();
if(m*n != r*c)//Do not process if the number of matrix elements is not equal
return mat;
vector<vector<int>> result;
int cx=0;//计数,每一行c个元素
vector<int> tmp(c);//存储一行数据
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
tmp[cx++] = mat[i][j];
if(cx==c)//tmpWhen a row is full, it is pushed into a new matrix
{
cx=0;
result.push_back(tmp);
}
}
}
return result;
}
};
矩阵的第i行jThe elements of the column are subscripted as :i*m+j
所以下标为index时,i=index/m,j=index%m
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
int m = mat.size();
int n = mat[0].size();
if(m*n != r*c)
return mat;
vector<vector<int>> result(r,vector<int>(c));
for(int i=0;i<m*n;i++)
{
result[i/c][i%c] = mat[i/n][i%n];
}
return result;
}
};
边栏推荐
- 【yolov7系列二】正负样本分配策略
- Replay Online Traffic Tool - GoReplay
- 电视处理器a53和a55哪个厉害(cortexa55处理器好吗)
- 你会看 MySQL 的执行计划(EXPLAIN)吗?
- 第十九届同济大学程序设计竞赛暨高校网络友谊赛 G-归零(可持久化权值线段树)
- The raised platform system based on JSP&Servlet implementation
- 软件测试架构师的工作日常
- Based on domestic, link global | schneider electric "industrial SI alliance partners hand in hand" to the industry in the future
- APP为什么用JSON协议与服务端交互:序列化相关知识
- MySQL 是如何实现 ACID 的?
猜你喜欢

测试日报怎么写 ?

基于Flink CDC打通数据实时入湖

A review of deep learning for beginners!

【微服务】(十六)—— 分布式事务Seata

Chinese Internet technology companies were besieged by wolves. Google finally suffered a severe setback and its profits fell sharply. It regretted promoting the development of Hongmeng...

交叉编译工具链的安装和配置过程
令人难以置信的DeepMind数据库现在包括了科学界已知的几乎所有蛋白质

即刻体验 | 借助 CTS-D 进一步提升应用设备兼容性

arcgis中编码方式改变引起的shp文件乱码、字符截断问题处理

Replay Online Traffic Tool - GoReplay
随机推荐
第十九届同济大学程序设计竞赛暨高校网络友谊赛 G-归零(可持久化权值线段树)
C语言 5:bool类型,关系表达式,逻辑表达式,分支语句,函数调用机制,break,continue,goto,return/exit跳转语句
Violence recursion to dynamic programming 02 (very clever game of CARDS)
How to return all prime factors of a number?
接口和抽象
arcpy脚本制作arcgis工具箱注意事项
国产手机将用户变成它们的广告肉鸡,难怪消费者都买iPhone了
字典树笔记(自用)
立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
这个 MySQL bug,99% 的人会踩坑!
Why do strings use the final keyword
面试官:生产环境中 CPU 利用率飙高怎么办?
【yolov7系列二】正负样本分配策略
Zhaoqi Technology creates a platform for overseas high-level talent introduction, corporate project docking, and event roadshows
正则、grep/egrep、sed、awk
xss内容总结
【LeetCode】88. 合并两个有序数组
你会看 MySQL 的执行计划(EXPLAIN)吗?
The reason for Apple's official price reduction has been found, and it is also facing declining sales and even inventory problems
使用Xshell和Xftp7跑学校服务器记录