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 ...
随机推荐
- (效果三)js实现选项卡切换
开发了很久的小程序,在接到一个h5移动端页面的时候,很多原生的东西都忘了,虽然说我们随着工作经验的增加,处理业务逻辑的能力在提高,但是基础的东西如果长时间不用,也会逐渐忘记.所以以后会经常总结原生的一 ...
- CMCC有限的访问权限如何解决
最近两天一直出现这个问题,连接CMCC-EDU的时候就是连接不上,提示有限的访问权限,什么诊断和修复IP自动获取都不管用,就是连接不上.怎么说本人也是一个电脑迷,越到这样不靠谱的问题确实不知道如何解决 ...
- 安装PL/SQL客户端来访问操作步骤
1 2 3.改变路径 4 5 6 7 8 9.选择注册机 10
- html页面中如何设置当光标移到一个固定区域时其形状变成手型,移出时恢复
在除了IE6的情况下,可以通过CSS的:hover伪类来实现: 假如你想设定的固定区域为:<div id="test"></div>,那么只需要在CSS样式 ...
- 一起来看CORE源码(一) ConcurrentDictionary
先贴源码地址 https://github.com/dotnet/corefx/blob/master/src/System.Collections.Concurrent/src/System/Col ...
- Switch能否使用String做参数
在Java语言中Swith可以使用参数类型有:Only convertible int values, strings or enum variables are permitted 可以自动转换为整 ...
- HashMap源码分析(基于JDK1.6)
在Java集合类中最常用的除了ArrayList外,就是HashMap了.本文尽自己所能,尽量详细的解释HashMap的源码.一山还有一山高,有不足之处请之处,定感谢指定并及时修正. 在看Hash ...
- 两种方式创建Maven项目【方式一】
经常使用maven进行项目的管理,今天整理两种方式创建maven项目及创建过程中碰到的问题怎么解决: 方式一: 1.新建maven项目,点击下一步. 2.勾选Create a simple proje ...
- python3 安装 past 包
python3 安装 past 包 $ pip install future 错误现象 缺乏包的引用 from past.types import unicode 参考链接 https://pypi. ...
- H5浏览器播放RTMP直播流
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...