【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 ...
随机推荐
- 123457123457#0#-----com.threeapp.PaoPaoLong01-----泡泡龙大作战01
com.threeapp.PaoPaoLong01-----泡泡龙大作战01
- 开启windows ping端口功能
1.打开控制面板,选择程序 2.选择打开和关闭windows功能 3.将Tenet客户端选项勾上 4.运行栏中输入cmd,进入到命令窗口 5.输入命令 ping ip地址 端口号 ...
- PostgreSQL学习笔记——窗口函数
在学习窗口函数之前,我们新建一个Product表并往其中插入一些数据: drop table if exists Product; create table Product ( product_id ...
- 【Leetcode_easy】633. Sum of Square Numbers
problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...
- 最新 房天下java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.房天下等10家互联网公司的校招Offer,因为某些自身原因最终选择了房天下.6.7月主要是做系统复习.项目复盘.LeetCo ...
- TCP使用
TCP使用注意事项总结 目录 发送或者接受数据过程中对端可能发生的情况汇总 本端TCP发送数据时对端进程已经崩溃 本端TCP发送数据时对端主机已经崩溃 本端TCP发送数据时对端主机已经关机 某个连 ...
- Google 安装 Elasticsearch-head 插件
下载插件:https://github.com/liufengji/es-head 安装插件: google --->更多工具---->扩展程序 将解压的elasticsearch-hea ...
- 平衡树B树B+树红黑树
二叉树与二叉查找树的操作是必须要熟练掌握的,接下来说的这些树实现起来很困难,所以我们重点去了解他们的特点. 一.平衡二叉查找树与红黑树 平衡树AVL:追求绝对的高度平衡,它具有稳定的logn的高度,因 ...
- js实现——鼠标移动时跟随着一连的小图片
首先放置一连的image <body> <div><img src="yezi.png" alt="tu"></div ...
- Centos7.3安装sonarqube
安装使用sonarqube 前置依赖 mysql 5.6 or 5.7 jdk 1.8 1.下载 https:/ ...