当前位置:网站首页>Leetcode notes 566. Reshaping the matrix
Leetcode notes 566. Reshaping the matrix
2022-07-28 13:24:00 【Lyrig~】
Leetcode note 566. Reshaping the matrix
Title Description
Topic linking
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:
Input :mat = [[1,2],[3,4]], r = 1, c = 4
Output :[[1,2,3,4]]
Example 2:
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
Ideas
It's pure simulation
Code
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m = nums.size();
int n = nums[0].size();
if (m * n != r * c) {
return nums;
}
vector<vector<int>> ans(r, vector<int>(c));
for (int x = 0; x < m * n; ++x) {
ans[x / c][x % c] = nums[x / n][x % n];
}
return ans;
}
};
Be careful
To learn vector Initialization method of , This is very important , Can save time .
边栏推荐
- 2020-12-27
- C语言学生成绩管理系统详解[通俗易懂]
- I copied the bottom of the liquidated NFT, but was locked by opensea
- 10、 Kubernetes scheduling principle
- 8、 Kubernetes network and load balancing
- Risk analysis of option trading
- [June 28 event preview] low code Summit
- LeetCode·每日一题·1331.数组序号转换·离散化
- How to use databricks for data analysis on tidb cloud | tidb cloud User Guide
- Black cat takes you to learn EMMC Protocol Part 24: detailed explanation of EMMC bus test program (cmd19 & cmd14)
猜你喜欢

【C语言易错点】第4篇:结构体在内存中存储规则详讲

leetcdoe-342. 4的幂

Pointnet++ Chinese Translation
![[error prone points of C language] Part 4: detailed rules for storing structures in memory](/img/87/555e6db40f23b5bd4919bd7bdce776.png)
[error prone points of C language] Part 4: detailed rules for storing structures in memory

【嵌入式C基础】第5篇:原码/反码/补码

Form for real-time custom verification

【嵌入式C基础】第2篇:进制转换与BCD编码

Is jetpack compose completely out of view?

Leetcode · daily question · 1331. array sequence number conversion · discretization

Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
随机推荐
[FPGA]: ISE generates MCS file and burning process
Understanding of vite2
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
Aragon creates Dao polygon BSC test network
Chinese translation of pointnet:deep learning on point sets for 3D classification and segmentation
How to improve deep learning performance?
Resolve browser password echo
[embedded C foundation] Part 9: basic usage of C language pointer
[FPGA] joint simulation of vivado and Modelsim
butterfly spreads
验证码暴力破解测试[通俗易懂]
Risk analysis of option trading
Shell基础概念和变量
Leetcode-190. inverting binary bits
What is the optimization method of transaction and database
Android工程师,如何使用Kotlin提供生产力?
Comments are not allowed in JSON
leetcode-136.只出现一次的数字
ES6 null merge operator (?)
How does kotlin help you avoid memory leaks?