LeetCode Reshape the Matrix
原题链接在这里:https://leetcode.com/problems/reshape-the-matrix/#/description
题目:
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.
题解:
利用count/c 和 count%c找到nums元素在结果matrix中的位置.
Time Complexity: O(m*n). m = nums.length, n = nums[0].length.
Space: O(m*n). res space.
AC Java:
public class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
if(nums == null || nums.length == 0 || nums[0].length == 0){
return nums;
}
int m = nums.length;
int n = nums[0].length;
if(m*n != r*c){
return nums;
}
int [][] res = new int[r][c];
int count = 0;
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
res[count/c][count%c] = nums[i][j];
count++;
}
}
return res;
}
}
LeetCode Reshape the Matrix的更多相关文章
- [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 new o ...
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- LeetCode 566. 重塑矩阵(Reshape the Matrix)
566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
- 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&Python] Problem 566. 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 (C++)
题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...
随机推荐
- MongoDB环境配置
在官网上下载MongoDB可执行文件安装在电脑上后,想要运行需先安装路径下新建一个data文件夹,再在里面新建db文件夹用户存放数据库文件和相关配置. 在bin目录里面运行命令行: 这样MongoDB ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:根据产品类别显示菜单分类和分页
路径访问的几种方式和分页效果 显示其它类别的效果和多数据分页效果 默认访问网站路径效果和多数据分页效果 URL路径访问可页面 http://localhost:5339/stationery http ...
- 对matrix,dataframe的操作函数
1.每行(列)的平均值:rowMeans() ; colMeans() 输入:数值型矩阵:数值型数据框 输出:向量 2.每行(列)的总和:rowSums() ;colSums() 输入:数值型矩阵,数 ...
- R和Python小数的保留
R: 1.保留几位有效数字: signif(x,digits) 2.保留几位小数: round(x,digits) Python: 1.“%.2f”%a
- 发现一个小坑的地方,unity的协程,想要停止,必须以字符串启动
今天想要停止一个协成,发现调用 StopCoroutine(ShowDebug()); 竟然不管用,后来看了文档才知道,原来想要停止协成,必须用字符启动协程 StartCoroutine(" ...
- 斯坦福机器学习视频笔记 Week9 异常检测和高斯混合模型 Anomaly Detection
异常检测,广泛用于欺诈检测(例如“此信用卡被盗?”). 给定大量的数据点,我们有时可能想要找出哪些与平均值有显着差异. 例如,在制造中,我们可能想要检测缺陷或异常. 我们展示了如何使用高斯分布来建模数 ...
- iBatis的Settings节点参数详解[转]
(1) cacheModelsEnabled: 是否启用SqlMapClient上的缓存机制.建议设为"true".默认值为“true”. (2) enhancementEnabl ...
- ssm文件上传下载比较详细的案例
背景:ssm框架 接下来,我会介绍单文件上传,下载,多文件的上传,下载,使用ajax进行文件的上传下载,和普通的表单提交的文件上传下载. 只要做项目,总是少不了文件的操作,好了废话不多说,直接上代码! ...
- LeetCode——Construct Binary Tree from Preorder and Inorder Traversal
Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...
- java arrays类学习
java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的. 具有以下功能: (1)给数组赋值:通过fill方法. (2)对数组排序:通过sort方法,按升序. (3)比较数组:通 ...