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.
分析:将给定的数组重新排列,若维数不一致,直接返回原数组
思路:首先判定维数是否一致,若一致,重新赋值即可。
JAVA CODE
class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
if(nums.length*nums[0].length!=r*c)
return nums;
int[][] mm = new int[r][c];
int r1 = 0, c1 = 0;
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums[i].length; j++){
if(c1==c){
r1++;
c1 = 0;
}
mm[r1][c1++] = nums[i][j];
}
}
return mm;
}
}
Reshape the Matrix的更多相关文章
- 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算法:Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- [LeetCode] Reshape the Matrix 重塑矩阵
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- [Swift]LeetCode566. 重塑矩阵 | 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 ...
- 566. Reshape the Matrix矩阵重排
[抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
随机推荐
- adb 安装apk 报错:Failure [INSTALL_FAILED_ALREADY_EXISTS]
遇到INSTALL_FAILED_ALREADY_EXISTS问题,直接通过adb install -r xxx.apk命令安装apk即可
- MySql数据库导入导出
1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 存放位置 比如: mysqldump -u root -p project > c:/a. ...
- Linux的netstat查看端口是否开放见解(0.0.0.0与127.0.0.1的区别)
linux运维都需要对端口开放查看 netstat 就是对端口信息的查看 # netstat -nltp p 查看端口挂的程序 [root@iz2ze5is23zeo1ipvn65aiz ~]# n ...
- Spring加载properties文件的属性的值
要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...
- 【打死树莓派】-树莓派3代jessie+Opencv-解决安装不了libgtk2.0-dev包问题
按照国际法先贴问题 Some packages could not be installed. This may mean that you have requested an impossible ...
- AOP TP框架有感
自学AOP感觉面向切面编程是一种利器,同时也是一种潜在的威胁.他就像一把手术刀,无论哪个器官有问题他都可以把他切开,修复它,但是使用的多了身体也会受不了... AOP应该算是面向对象的一种补充,但是, ...
- Web前端和后端之区分,以及…
原文地址:Web前端和后端之区分,以及面临的挑战[转]作者:joyostyle 在我们实际的开发过程中,我们当前这样定位前端.后端开发人员. 1)前端开发人员:精通JS,能熟练应用JQuery,懂CS ...
- Bootstrap框架的了解和使用(一)
前 言 Bootstrap 什么是 Bootstrap?Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JavaScrip ...
- 201521123111《Java程序设计》第8周学习总结
1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 附上ppt: 1.2 选做:收集你认为有用的代码片段 List strList = new ArrayList ...
- 201521123005 《Java程序设计》 第十一周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 Q1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) ...