Leetcode566.Reshape the Matrix重塑矩阵
在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。
给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的行数和列数。
重构后的矩阵需要将原始矩阵的所有元素以相同的行遍历顺序填充。
如果具有给定参数的reshape操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
示例 1:
输入: nums = [[1,2], [3,4]] r = 1, c = 4 输出: [[1,2,3,4]] 解释: 行遍历nums的结果是 [1,2,3,4]。新的矩阵是 1 * 4 矩阵, 用之前的元素值一行一行填充新矩阵。
示例 2:
输入: nums = [[1,2], [3,4]] r = 2, c = 4 输出: [[1,2], [3,4]] 解释: 没有办法将 2 * 2 矩阵转化为 2 * 4 矩阵。 所以输出原矩阵。
注意:
- 给定矩阵的宽和高范围在 [1, 100]。
- 给定的 r 和 c 都是正数。
class Solution {
public:
vector<vector<int> > matrixReshape(vector<vector<int> >& nums, int r, int c) {
int R = nums.size();
if(R == 0)
return nums;
int C = nums[0].size();
if(r * c != R * C)
return nums;
vector<vector<int> >res;
int cntR = 0;
int cntC = 0;
for(int i = 0; i < r; i++)
{
vector<int> temp;
for(int j = 0; j < c; j++)
{
temp.push_back(nums[cntR][cntC]);
if(cntC == C - 1)
{
cntC = 0;
cntR++;
}
else
{
cntC++;
}
}
res.push_back(temp);
}
return res;
}
};
Leetcode566.Reshape the Matrix重塑矩阵的更多相关文章
- [LeetCode] Reshape the Matrix 重塑矩阵
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- leetcode 566 Reshape the Matrix 重塑矩阵
参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...
- leetcode566. Reshape the Matrix
https://leetcode.com/problems/reshape-the-matrix/description/ public int[][] matrixReshape(int[][] n ...
- LeetCode 566. 重塑矩阵(Reshape the Matrix)
566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...
- [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- LeetCode 566. Reshape the Matrix (重塑矩阵)
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- C#LeetCode刷题之#566-重塑矩阵( Reshape the Matrix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3720 访问. 在MATLAB中,有一个非常有用的函数 resha ...
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- 566. Reshape the Matrix矩阵重排
[抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
随机推荐
- 出错提示:“Could not flush the DNS Resolver Cache: 执行期间,函数出了问题”的解决方法
在DNS解析中,出错提示:"Could not flush the DNS Resolver Cache: 执行期间,函数出了问题"的解决方法 . 由于公司网站空间更换了服务商. ...
- Js中的onblur和onfocus事件应用介绍
html页面中,诸如按钮.文本框等可视元素都具有拥有和失去焦点的事件,本文以文本框获得和失去焦点为例简单讲解onfocus和onblur的应用 html页面中,诸如按钮.文本框等可视元素都具有拥有和失 ...
- iOS界面动画特效
1.TableView的headView背景图片拉伸 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup aft ...
- 设置和修改Linux的swap分区大小
在Linux编译gcc时,遇到编译错误,究其根源是因为内存不足,这时通过修改swap大小解决了问题 相关操作如下: 1. 查看当前分区情况free -m 2. 增加 swap 大小, 2G 左右dd ...
- 《DSP using MATLAB》Problem 8.18
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Django的日常-路由层
目录 Django的日常-2 路由层 有名分组和无名分组 反向解析 路由的分发 Django的日常-2 路由层 我们之前已经接触过路由层,只是我们可能不知道他叫这个名字,实际上在Django里面路由层 ...
- jqGrid的subGrid子表格
使用完整jqGrid作为子表格 使用子表格,涉及到jqGrid的三个选项: subGrid :首先必须将jqGrid的subGrid选项设置为true,默认为false:当此项设为true的时候,Gr ...
- Js获取或计算时间的相关操作
//获取当前日期(年月日),如:2017-12-18 function getNowDate() { var dd = new Date(); var y = dd.getFullYear(); // ...
- tDQSS
tDQSS - DQS latching rising transitions to associated clock edges, as described on Table 41/42 of JE ...
- 使用 top instance 命令查看运行中 MaxCompute 作业
我们都知道,在 MaxCompute Console 里,可以使用下面的命令来列出运行完成的 instance 列表. show p|proc|processlist [from <yyyy-M ...