当前位置:网站首页>[数组]566. 重塑矩阵-简单
[数组]566. 重塑矩阵-简单
2022-07-05 03:36:00 【51CTO】
在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据。
给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。
重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。
如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
示例 1:
![[数组]566. 重塑矩阵-简单_数组](/img/3c/593156f5bde67bd56828106d7bed3c.png)
输入:mat = [[1,2],[3,4]], r = 1, c = 4
输出:[[1,2,3,4]]
示例 2:
![[数组]566. 重塑矩阵-简单_数组_02](/img/49/4c7d6984954381f6112ed888a7e8ea.png)
输入: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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/reshape-the-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
思路:
- 判断输入的矩阵个数和输出矩阵个数是否相等
- 为输出矩阵申请空间
- 遍历原始矩阵,将原始矩阵元素搬到输出矩阵
代码如下:
using
namespace
std;
class
Solution
{
public:
vector
<
vector
<
int
>>
matrixReshape(
vector
<
vector
<
int
>>
&
mat,
int
r,
int
c)
{
if (
mat.
empty())
{
return
mat;
}
int
m
=
mat.
size();
int
n
=
mat[
0].
size();
if (
m
*
n
!=
r
*
c)
{
return
mat;
}
std::vector
<
std::vector
<
int
>>
ans(
r,
std::vector
<
int
>(
c,
0));
int
t
=
0;
for (
int
i
=
0;
i
<
m;
++
i)
{
for (
int
k
=
0;
k
<
n;
++
k)
{
ans[
t
/
c][
t
%
c]
=
mat[
i][
k];
t
++;
}
}
return
ans;
}
};
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
多数几句
leetcode上下面的运行时间居然比我的还要快(我的12ms),简直离谱了,先看leetcode给的8ms的代码。
class
Solution {
public:
vector
<
vector
<
int
>>
matrixReshape(
vector
<
vector
<
int
>>&
mat,
int
r,
int
c) {
int
m
=
mat.
size();
int
n
=
mat[
0].
size();
int
num
=
0;
vector
<
int
>
s;
vector
<
vector
<
int
>>
mat1(
r,
vector
<
int
>(
c));
//初始化大小了才能赋值
if(
m
*
n
!=
r
*
c)
{
return
mat;
}
for(
int
i
=
0;
i
<
m;
i
++)
{
for(
int
j
=
0;
j
<
n;
j
++)
{
s.
push_back(
mat[
i][
j]);
}
}
for(
int
i
=
0;
i
<
r;
i
++)
{
for(
int
j
=
0;
j
<
c;
j
++)
{
mat1[
i][
j]
=
s[
num];
num
++;
}
}
return
mat1;
}
};
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
这个代码不仅比我多申请一个vector,而且还有元素的各种push_back,这些都是额外的开销,居然比我的还快,leetcode的时间判断是真的蛋疼。
当然上面我的代码也有可以优化的,我们知道CPU是有缓存的,如果缓存命中率高,那么程序运行的就能更快。如果可以减少计算,那么提高程序的运行效率。我们可以从最后的那个循环入手进行优化,优化后的代码如下:
using
namespace
std;
class
Solution
{
public:
vector
<
vector
<
int
>>
matrixReshape(
vector
<
vector
<
int
>>
&
mat,
int
r,
int
c)
{
if (
mat.
empty())
{
return
mat;
}
int
m
=
mat.
size();
int
n
=
mat[
0].
size();
int
total_count
=
m
*
n;
if (
total_count
!=
r
*
c)
{
return
mat;
}
std::vector
<
std::vector
<
int
>>
ans(
r,
std::vector
<
int
>(
c,
0));
for (
int
i
=
0;
i
<
total_count;
++
i)
{
ans[
i
/
c][
i
%
c]
=
mat[
i
/
n][
i
%
n];
t
++;
}
return
ans;
}
};
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
这个代码官方号称可以达到0ms,但是经过测试,1次是8ms,1次是4ms,蜜汁操作~~~
优化的2个点如下:
- 将2个for循环改成1个,可以增加cpu的命中率
- 提前计算m*n,嗯,这个微不足道~~~
边栏推荐
- [untitled]
- Technology sharing swift defense programming
- Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
- [an Xun cup 2019] not file upload
- 函数基础学习02
- [software reverse analysis tool] disassembly and decompilation tool
- Share the newly released web application development framework based on blazor Technology
- Mongodb common commands
- ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
- How to learn to get the embedding matrix e # yyds dry goods inventory #
猜你喜欢
![[groovy] string (string injection function | asBoolean | execute | minus)](/img/ea/bf1e6aa713cf54e29653e35b164560.jpg)
[groovy] string (string injection function | asBoolean | execute | minus)

SPI and IIC communication protocol
![[安洵杯 2019]不是文件上传](/img/f1/736eb5fe51c299e3152ca87895ee99.png)
[安洵杯 2019]不是文件上传

Subversive cognition: what does SRE do?

JWT vulnerability recurrence

Leetcode92. reverse linked list II

Mongodb common commands

【web源码-代码审计方法】审计技巧及审计工具

LeetCode146. LRU cache

Talk about the SQL server version of DTM sub transaction barrier function
随机推荐
[untitled]
Une question est de savoir si Flink SQL CDC peut définir le parallélisme. Si le parallélisme est supérieur à 1, il y aura un problème d'ordre?
Kubernetes - Multi cluster management
Ubantu disk expansion (VMware)
Delphi free memory
v-if VS v-show 2.0
Learning notes of raspberry pie 4B - IO communication (I2C)
[an Xun cup 2019] not file upload
Clickhouse同步mysql(基于物化引擎)
C file in keil cannot be compiled
Basic authorization command for Curl
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
001 chip test
[software reverse analysis tool] disassembly and decompilation tool
Ask, does this ADB MySQL support sqlserver?
New interesting test applet source code_ Test available
[luat-air105] 4.1 file system FS
It took two nights to get Wu Enda's machine learning course certificate from Stanford University
Pat class a 1160 forever (class B 1104 forever)
Difference between MotionEvent. getRawX and MotionEvent. getX