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 ...
随机推荐
- smarty变量调节器与函数
smarty自带了一些变量调节器与内置函数,都在libs/plugins目录下,变量调节器以modifier开头,函数以function开头,而且我们可以自定义变量调节器与函数,熟练运用之后会极大地提 ...
- iOS CMTimeMake 和 CMTimeMakeWithSeconds 学习
CMTime是专门用于标识电影时间的结构体,通常用如下两个函数来创建CMTime (1)CMTimeMake CMTime CMTimeMake ( int64_t value, //表示 当前视频播 ...
- rsh命令配置于使用
安装环境:一台centos6.10虚拟机,一台centos7.5虚拟机,全部使用root用户登录. 两台机器上都要安装rsh.rsh-server.xinetd包. 两台机器都要关闭防火墙并配置/et ...
- CSS3 3D旋转按钮对话框
在线演示 本地下载
- R的几个基础函数
本章目录: 1.路径和文件 2.数据转换 3.获得帮助 路径和文件: 1.工作路径: 显示当前路径:getwd() 设置路径:setwd(“绝对路径”) 2.目录: 创建目录:dir.create(& ...
- 谈谈 cci 与 i2c
cci的名字叫, camera control interface, 他由两部分组成,一是i2c ,而另一个部分是 gpio.也就是说,cci 包含i2c.一般情况下,我们只是用到了i2c 部分,没有 ...
- Java I/O 小结
主要内容: 一.输入流基类:InputStream 和 OutputStream(字节流). Reader 和 Writer(字符流) 二.文件字节流:FileInputStream和FileOutp ...
- HDU 3466 Proud Merchants 排序 背包
题意:物品有三个属性,价格p,解锁钱数下线q(手中余额>=q才有机会购买该商品),价值v.钱数为m,问购买到物品价值和最大. 思路:首先是个01背包问题,但购买物品受限所以应先排序.考虑相邻两个 ...
- centos安装zabbix监控服务器端
首先安装zabbx 依赖yum install net-snmp-devel libxml2-devel libcurl-devel -y 下载zabbix 源码包wget https://ncu.d ...
- Java -- JDBC mysql读写大数据,文本 和 二进制文件
1. 往mysql中读写字符文本 public class Demo1 { /* 创建数据库 create database LOBTest; use LOBTest; create table te ...