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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么用程序语言写出来
[一句话思路]:
形象思考一下:先加col,再加row
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
nums[0]是指同一列
[一刷]:
- 不会写重排:col满了之后,直接清0就行
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
重排:col满了之后,直接清0就行
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
重排
if (col == c) {
col = 0;
row++;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
//ini
int m = nums.length;
int n = nums[0].length;
int[][] result = new int[r][c]; //cc
if (m * n != r * c) {
return nums;
} //for loop,add
int col = 0;
int row = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result[row][col] = nums[i][j];
col++; if (col == c) {
col = 0;
row++;
}
}
} //return res
return result;
}
}
566. Reshape the Matrix矩阵重排的更多相关文章
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- 566. Reshape the Matrix - LeetCode
Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ...
- 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 解题报告
题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
- LeetCode 566. Reshape the Matrix (C++)
题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...
- 【leetcode】566. Reshape the Matrix
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
- [Array] 566. 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 ...
- [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 ...
随机推荐
- Hadoop集群中节点角色定义
Hadoop分别从两个角度将主机划分为两种角色. 最基本的划分原则为Master和Slave,即主人和奴隶: 第一,从HDFS的角度,将主机划分为NameNode和DataNode(在分布式文件系统中 ...
- WC2019 T1 数树
WC2019 T1 数树 传送门(https://loj.ac/problem/2983) Question 0 对于给定的两棵树,设记两颗树 \(A,B\) 的重边数量为 \(R(A,B)\),那么 ...
- Python之contextlib库及源码分析
Utilities for with-statement contexts __all__ = ["contextmanager", "closing", &q ...
- 动态可缓存的内容管理系统(CMS)
摘要:内容管理系统(CMS)在各大商业站点和门户站点中扮演着重要的角色,是内容有效组织和快速发布极为重要的基础平台.目前主流的内容发布系统都使用静态页面进行内容发布,在我们的实际使用过程中我们深切的感 ...
- 一个工程实例来学习 Makefile
March 3, 2015 8:19 PM Makefile 文件的编写 学习前的准备 需要准备的工程目录结构如下: . ├── add │ ├── add_float.c │ ├── add ...
- lvs之 lvs+nginx+tomcat_1、tomcat_2+redis(lvs dr 模式)
前提:已经安装好 lvs+nginx+tomcat_1.tomcat_2+redis环境 ,可参考 (略有改动,比如tomcat_1.tomcat_2安装在两台机器上,而不是单机多实例 ,自行稍稍变动 ...
- vue-cli结构
|-- build // 项目构建(webpack)相关代码 | |-- build.js // 生产环境构建代码 | |-- check-version.js // 检查node.npm等版本 | ...
- 51nod 1362 搬箱子——[ 推式子+组合数计算方法 ] [ 拉格朗日插值 ]
题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1362 方法一: 设 a 是向下走的步数. b 是向右下走的步数. c 是向下走 ...
- Unity GUI自适应屏幕分辨率(一)布局自适应
这里我们先谈第一个问题坐标矩阵变化实现布局自适应. 选取基准尺寸 通常你需要选择一个基准的屏幕尺寸,象现在开发的应用也需要跨平台在iOS(iPhone/iPad)/Android都可以运行,我这边选取 ...
- 使用Spring和Tomcat发布CXF SOAP WebService
上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ...