当前位置:网站首页>[array]566 Reshape the matrix - simple
[array]566 Reshape the matrix - simple
2022-07-05 03:50:00 【51CTO】
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 .
Example 1:
![[ Array ]566. Reshaping the matrix - Simple _ Array](/img/3c/593156f5bde67bd56828106d7bed3c.png)
Input :mat = [[1,2],[3,4]], r = 1, c = 4
Output :[[1,2,3,4]]
Example 2:
![[ Array ]566. Reshaping the matrix - Simple _ Array _02](/img/49/4c7d6984954381f6112ed888a7e8ea.png)
Input :mat = [[1,2],[3,4]], r = 2, c = 4
Output :[[1,2],[3,4]]
Tips :
m == mat.length
n == mat[i].length
1 <= m, n <= 100
-1000 <= mat[i][j] <= 1000
1 <= r, c <= 300
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reshape-the-matrix
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
Ideas :
- Judge whether the number of input matrices and the number of output matrices are equal
- Apply for space for the output matrix
- Traverse the original matrix , Move the original matrix elements to the output matrix
The code is as follows :
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.
A few more sentences
leetcode The running time of the upper and lower parts is even faster than mine ( my 12ms), It's ridiculous , First look at leetcode Given 8ms Code for .
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));
// The value can only be assigned after the initialization size
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.
This code is not only one more than me vector, And there are all kinds of elements push_back, These are extra expenses , It's faster than mine ,leetcode It's really painful to judge the time .
Of course, my code above can also be optimized , We know CPU There is a cache , If the cache hit rate is high , Then the program can run faster . If you can reduce the calculation , Then improve the efficiency of the program . We can optimize from the last cycle , The optimized code is as follows :
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.
This code is officially known to achieve 0ms, But it's been tested ,1 Next is 8ms,1 Next is 4ms, Honey operation ~~~
Optimization of the 2 The first point is as follows :
- take 2 individual for Cycle to 1 individual , Can increase cpu shooting
- Calculate ahead of time m*n, Um. , This is trivial ~~~
边栏推荐
- 040. (2.9) relieved
- KVM virtualization
- MindFusion.Virtual Keyboard for WPF
- JWT vulnerability recurrence
- How can we truncate the float64 type to a specific precision- How can we truncate float64 type to a particular precision?
- ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
- Clean up PHP session files
- 天干地支纪年法中为什么是60年一个轮回,而不是120年
- Containerization Foundation
- v-if VS v-show 2.0
猜你喜欢
![[groovy] string (string injection function | asBoolean | execute | minus)](/img/ea/bf1e6aa713cf54e29653e35b164560.jpg)
[groovy] string (string injection function | asBoolean | execute | minus)

Smart pointer shared_ PTR and weak_ Difference of PTR
![Quick start of UI component development of phantom engine [umg/slate]](/img/8b/cee092ec1ab105a7e234143bd56861.jpg)
Quick start of UI component development of phantom engine [umg/slate]

ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)

一文带你了解BI的前世今身与企业数字化转型的关系

我就一写代码的,王总整天和我谈格局...

【无标题】

Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
![[positioning in JS]](/img/f1/02ce74fadc1f7524c7abca9db66c71.jpg)
[positioning in JS]
![[C language] address book - dynamic and static implementation](/img/eb/07e7a32a172e5ae41457cf8a49c130.jpg)
[C language] address book - dynamic and static implementation
随机推荐
Kubernetes - Multi cluster management
【PHP特性-变量覆盖】函数的使用不当、配置不当、代码逻辑漏洞
[an Xun cup 2019] not file upload
How to use jedis of redis
Kubernetes - identity and authority authentication
面试汇总:这是一份全面&详细的Android面试指南
ICSI213/IECE213 Data Structures
[wp]bmzclub几道题的writeup
[move pictures up, down, left and right through the keyboard in JS]
花了2晚,拿到了吴恩达@斯坦福大学的机器学习课程证书
speed or tempo in classical music
Smart pointer shared_ PTR and weak_ Difference of PTR
C # use awaiter
IronXL for .NET 2022.6
程序员的视力怎么样? | 每日趣闻
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
[untitled]
Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
Clickhouse materialized view
glibc strlen 实现方式分析