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:

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.
 public class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int row = nums.length;
int col = nums[0].length;
int[][] res = new int[r][c];
if(row * col != r * c) {
return nums;
}
for(int i=0; i<r*c; i++) {
res[i/c][i%c] = nums[i/col][i%col];
}
return res;
}
}

数组和矩阵(2)——Reshape the Matrix的更多相关文章

  1. C#LeetCode刷题之#566-重塑矩阵( Reshape the Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3720 访问. 在MATLAB中,有一个非常有用的函数 resha ...

  2. LeetCode 566. 重塑矩阵(Reshape the Matrix)

    566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...

  3. Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)

    Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...

  4. [LeetCode] Reshape the Matrix 重塑矩阵

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  5. [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  6. LeetCode 566. Reshape the Matrix (重塑矩阵)

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  7. 566. Reshape the Matrix矩阵重排

    [抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...

  8. python数组和矩阵使用总结

    python数组和矩阵使用总结 1.数组和矩阵常见用法 Python使用NumPy包完成了对N-维数组的快速便捷操作.使用这个包,需要导入numpy. SciPy包以NumPy包为基础,大大的扩展了n ...

  9. [Array] 566. Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

随机推荐

  1. maven设置------settings.xml文件学习

    https://blog.csdn.net/tomato__/article/details/13025187 快速预览 maven的配置文件为settings.xml,在下面路径中可以找到这个文件, ...

  2. networkx如何将图写到邻接矩阵里?

    nx.write_adjlist(G1,graph_filename1)#生成的是二进制文件nx.write_adjlist(G2,graph_filename2)

  3. The server of Apache (三)——网页优化

    在企业中,部署apache后只采用默认的配置参数,会有很多问题,因为那些配置都是针对以前服务器配置的. 一.网页压缩 1.介绍 配置apache的网页压缩功能,是使用Gzip压缩算法来对apache服 ...

  4. 关于 SimpleMembership 中 CreateDate 的问题

    使用 WebMatrix.WebData.WebSecurity.CreateUserAndAccount(model.UserName, model.Password,                ...

  5. Qt 学习之路 2(47):视图选择

    Qt 学习之路 2(47):视图选择 豆子 2013年3月28日 Qt 学习之路 2 34条评论 选择是视图中常用的一个操作.在列表.树或者表格中,通过鼠标点击可以选中某一项,被选中项会变成高亮或者反 ...

  6. OC语法

    ``` int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSString *S= ...

  7. 006 Android XML 文件布局及组件属性设置技巧汇总

    1.textview 组件文本实现替换(快速实现字符资源的调用) android 应用资源位置在 project(工程名)--->app--->res--->values 在stri ...

  8. elementui 给 左侧功能栏 加上 el-scroll 时 , el-main 会 挤压 左侧导航栏..

    解决方法... el-main 给上 style='width:0'........... 并不会影响 el-main 布局...

  9. ssm框架中的乱码问题的解决

    在搭建ssm框架过程中可能会出现乱码的问题,解决的最好方法就是不管在哪儿都设置为utf-8: 统一编码格式,所有的都设置为utf-8: 首先的就是将所有的请求设置为utf-8: 设置post请求的编码 ...

  10. c#工具类之Bitmap缩放帮忙类

    using System.Drawing; using System.Drawing.Drawing2D; /// <summary> /// BitmapHelper /// </ ...