【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.
解析
重组矩阵
给一个矩阵,给一组行列数,按照给定的行列数,重组给出矩阵,使满足新的行列
若给出矩阵不能满足新的行列,输出原矩阵
思路
就是循环赋值新矩阵即可
我的解法
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的更多相关文章
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
- 【LeetCode】519. Random Flip Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-fl ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【LeetCode】756. Pyramid Transition Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】756. Pyramid Transition Matrix
题目如下: We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, ...
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- 简介Python中用于处理字符串的center()方法
简介Python中用于处理字符串的center()方法 这篇文章主要介绍了简介Python中用于处理字符串的center()方法,是Python入门中的基础知识,需要的朋友可以参考下 center() ...
- Zabbix设置触发器调用远程主机脚本实现触发告警后自动启动自愈功能
参考:https://www.cnblogs.com/xiami-xm/p/8929163.html 当zabbix添加触发器后触发告警后可以设置发送邮件及短信告警,但是恢复故障需要运维人员收到告警以 ...
- PHP实现无限极分类的两种方式,递归和引用
面试的时候被问到无限极分类的设计和实现,比较常见的做法是在建表的时候,增加一个PID字段用来区别自己所属的分类 $array = array( array('id' => 1, 'pid' =& ...
- 共享文件word / excel /ppt 被用戶自己锁定无法编辑-解決方法
共享文件word / excel /ppt 被用戶自己鎖定無法編輯,但用戶嘗試過關閉所有文件和重啓過系統,依然無法編輯. 搜到解決方法: Just in case someone looking fo ...
- P1020 【导弹拦截】
题目连接嘤嘤嘤~~ 这个题目还是比较难的(至少对我来说是酱紫的嘤嘤嘤).. 第一问,看题解好像用的都是DP,但其实可以用二分,求最长不上升子序列,因为只要输出答案,不用输出方案,时间复杂度n leg( ...
- 【数据库开发】Redis数据库设置密码
redis配置密码1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 ? 1 #requirepass foobared 去掉行前的注释 ...
- 从零开始学游戏开发(一):下载与安装UE4游戏引擎
如何下载和安装虚幻引擎 下载Epic Games Launcher 步骤 百度搜索"what is ue4" 点击第一个搜索结果,进入ue4官网 进入官网首页,点击右上角下载 创建 ...
- [转帖]Oracle数据库lob大对象数据类型字段总结,值得收藏
Oracle数据库lob大对象数据类型字段总结,值得收藏 原创 波波说运维 2019-07-11 00:02:00 https://www.toutiao.com/i67108943269703357 ...
- [转帖]删除一张大表时为什么undo占用空间接近原表两倍?
删除一张大表时为什么undo占用空间接近原表两倍? https://www.toutiao.com/i6736735016492990983/ 原创 波波说运维 2019-09-22 00:01:00 ...
- todo---jaxen