LeetCode 566. Reshape the Matrix (重塑矩阵)
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
Example 1:
Input:
nums =
[[1,2],
[3,4]]
r = 1, c = 4
Output:
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
Example 2:
Input:
nums =
[[1,2],
[3,4]]
r = 2, c = 4
Output:
[[1,2],
[3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
Note:
- The height and width of the given matrix is in range [1, 100].
- The given r and c are all positive.
题目标签:Array
这道题目给了我们一个matrix 2d array, 让我们根据给的r 和 c 来重新塑造一个新的矩阵。那么首先我们先判断一下,给的r 和 c 能不能用来塑造一个2d array。比较一下总数就可以了。接下来我们就重塑一个2d array 根据给的r 和c,遍历原来的nums, 设两个int,col 和 row, 对于旧的nums 2d array,当我们每次遍历一行里的每一个数字,我们每次col++,意思就是增加列。一旦当我们自己数的col 列 = c 给的列的时候,我们就更新col 列 = 0, 并且row++。 每一次的数字利用自己计数的row 和 col 代入, res[row][col]。
Java Solution:
Runtime beats 38.21%
完成日期:05/10/2017
关键词:Array
关键点:自己来计数row and column, 并存入每一个int到新的2d array里面
public class Solution
{
public int[][] matrixReshape(int[][] nums, int r, int c)
{
// check if new array is possible.
if(nums.length * nums[0].length != r * c)
return nums; int[][] res = new int[r][c]; int col=0;
int row=0;
for(int i=0; i<nums.length; i++)
{
for(int j=0; j<nums[i].length; j++)
{
if(col == c)
{
col = 0;
row++;
}
res[row][col] = nums[i][j];
col++;
} } return res;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 566. Reshape the Matrix (重塑矩阵)的更多相关文章
- leetcode 566 Reshape the Matrix 重塑矩阵
参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- [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 解题报告
题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
- LeetCode 566. Reshape the Matrix (C++)
题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...
- Leetcode566.Reshape the Matrix重塑矩阵
在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的 ...
- LeetCode - 566. Reshape the Matrix (C++) O(n)
1. 题目大意 根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数.如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵.否则,重塑矩阵.其中两个矩阵中的数据顺序不变(先行后列). ...
- 566. Reshape the Matrix - LeetCode
Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ...
- 【leetcode】566. Reshape the Matrix
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
随机推荐
- Java:实现对象的比较 comparable接口和comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- 银河麒麟操作系统打开VMware报vmmon无法编译
使用银河麒麟操作系统打开VMware可能会报vmmon无法编译 这个时候... 将/usr/src/linux-headers-xxx/include/miscdevice.h第71行void改为in ...
- Java单链表实现
/** * * 单链表基本操作 * * @author John * */ class LinkList { private Node first; private int pos = 0; publ ...
- Spring配置属性文件
在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...
- Activiti-04-.Spring integration
ProcessEngineFactoryBean <beanid="processEngineConfiguration"class="org.activiti.s ...
- Thinkphp3.2版本使用163邮箱发(验证码)邮件
今天忽然想写一个用户修改密码的功能,又没有短信接口,只能选择用邮箱发送验证码啦,穷啊,没办法,哈哈,以下为正文. ------------------------------------------- ...
- 导航栏 .active激活
参考 :使用angularjs写一个导航栏控制器 1.点击其中一个li其他的class不影响 2.点击其中一个li其他的class影响 <!DOCTYPE html> <html l ...
- hdu1166 敌兵布阵
敌兵布阵 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动 ...
- python GUI实战项目——tkinter库的简单实例
一.项目说明: 本次通过实现一个小的功能模块对Python GUI进行实践学习.项目来源于软件制造工程的作业.记录在这里以复习下思路和总结编码过程.所有的源代码和文件放在这里: 链接: https:/ ...
- js自执行函数写法
(1)写法1 (function(){ //函数内容 })() (2)写法2 (function(){ //函数内容 }())