原题

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.

解析

重组矩阵

给一个矩阵,给一组行列数,按照给定的行列数,重组给出矩阵,使满足新的行列

若给出矩阵不能满足新的行列,输出原矩阵

思路

就是循环赋值新矩阵即可

我的解法

    public static int[][] matrixReshape(int[][] nums, int r, int c) {
if (nums == null || nums.length <= 0 || nums[0].length <= 0 || nums.length * nums[0].length != r * c) {
return nums;
}
int[][] newMatrix = new int[r][c];
//用k,l表示新矩阵的行列下标,k<r,l<c
int k = 0, l = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
if (l >= c) {
l = 0;
k++;
}
if (k >= r) {
break;
}
newMatrix[k][l++] = nums[i][j];
}
}
return newMatrix;
}

最优解

使用了除法和求余数,分别表示行列,简化了代码

public int[][] matrixReshapeOptimized(int[][] nums, int r, int c) {
int n = nums.length, m = nums[0].length;
if (r * c != n * m) {
return nums;
}
int[][] res = new int[r][c];
for (int i = 0; i < r * c; i++) {
res[i / c][i % c] = nums[i / m][i % m];
}
return res;
}

【leetcode】566. Reshape the Matrix的更多相关文章

  1. 【LeetCode】566. Reshape the Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...

  2. 【LeetCode】519. Random Flip Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-fl ...

  3. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  4. 【leetcode】 Search a 2D Matrix (easy)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. 【leetcode】519. Random Flip Matrix

    题目如下: You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix whe ...

  6. 【LeetCode】756. Pyramid Transition Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  7. 【leetcode】756. Pyramid Transition Matrix

    题目如下: We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, ...

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

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

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. python基础之内置模块(二)

    configparser configparser用来对特定格式的文件进行解析处理,比如ha-proxy,rsync,samba配置文件等等均可.来个简单的文件先看下: [section1] #节点 ...

  2. Python使用设计模式中的责任链模式与迭代器模式的示例

    Python使用设计模式中的责任链模式与迭代器模式的示例 这篇文章主要介绍了Python使用设计模式中的责任链模式与迭代器模式的示例,责任链模式与迭代器模式都可以被看作为行为型的设计模式,需要的朋友可 ...

  3. Flutter 弹出键盘屏幕溢出问题

    在使用输入框获取焦点,弹出键盘的时候,会导致屏幕溢出, 解决办法: resizeToAvoidBottomPadding: false, //输入框抵住键盘 return Scaffold( appB ...

  4. laravel的ORM转为原生sql

    注:mysql测试成功,mongoDB测试失败//将laravel的ORM转为原生sql $SubProfits为laravel的ORM对象 $SubProfits = model::where('i ...

  5. python3.7 完美安装

    在安装python3.7的过程中,我发现如果不加注意,pip3是无法被安装的.而这就不能算是完整安装python3了. 所以,我总结一下,如何完美安装python3.7.   依赖 yum insta ...

  6. 【基本优化实践】【1.6】在sql server修改且移动数据库文件位置

    在master数据库中,SQL Server提供系统扩展的存储过程,其中有一些存储过程的命名以xp_开头,用于处理操作系统的文件. 一,判断文件是否存在 存储过程sys.xp_fileexist 用于 ...

  7. 什么是SSH 以及常见的ssh 功能

    什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录.如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会泄露. ...

  8. IP地址 子网掩码 网络地址 主机地址 广播地址

    1.一定要明白各自的概念分别表示什么 IP地址:IP地址是用来识别网络上的设备,因此,IP地址是由网络地址与主机地址两部分所组成. 子网掩码:子网掩码不能单独存在,它必须结合IP地址一起使用.子网掩码 ...

  9. 病毒 | wordpress网站内容被篡改、自动跳转、变全英文的解决办法

    去年10月开始,网站经常有文章被莫名其妙的篡改,而且后面还经常出现跳转到色情网站的问题,让人烦不胜烦,困扰了好几个月,最后终于解决了.这里特次记录和总结下此次恼人的事件. 时间:2018年10月 问题 ...

  10. python学习--13 基本数据类型 2

    接上次补充: s = "username\temail\tpassword\naaa\taa@qq.com\t123\nusername\temail\tpassword\naaa\taa@ ...