[LeetCode] 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.
Note:
- The height and width of the given matrix is in range [1, 100].
- The given r and c are all positive.
这道题让我们实现矩阵大小的重塑,也就是实现 Matlab 中的 reshape 函数,博主也经常使用 matlab,对这个函数还是比较的熟悉的。对于这种二维数组大小重新非配的问题的关键就是对应位置的坐标转换,最直接的办法就是先把原数组拉直,变成一条直线,然后再组成新的数组。所以这道题我们先判断给定数组是否能重塑成给定的大小,就是看两者的元素总数是否相同,直接行数乘以列数即可,然后我们新建一个目标大小的数组,并开始遍历,对于每个位置,我们先转为拉直后的一维坐标,然后在算出在原数组中的对应位置赋值过来即可,参见代码如下:
解法一:
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int m = nums.size(), n = nums[].size();
        if (m * n != r * c) return nums;
        vector<vector<int>> res(r, vector<int>(c));
        for (int i = ; i < r; ++i) {
            for (int j = ; j < c; ++j) {
                int k = i * c + j;
                res[i][j] = nums[k / n][k % n];
            }
        }
        return res;
    }
};
下面这种方法整体思路和上面没啥区别,但是只使用了一个循环,直接就是遍历拉直后的一维数组的坐标,然后分别转换为两个二维数组的坐标进行赋值,参见代码如下:
解法二:
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int m = nums.size(), n = nums[].size();
        if (m * n != r * c) return nums;
        vector<vector<int>> res(r, vector<int>(c));
        for (int i = ; i < r * c; ++i) {
            res[i / c][i % c] = nums[i / n][i % n];
        }
        return res;
    }
};
参考资料:
https://leetcode.com/problems/reshape-the-matrix/
https://discuss.leetcode.com/topic/87851/java-concise-o-nm-time
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Reshape the Matrix 重塑矩阵的更多相关文章
- Leetcode566.Reshape the Matrix重塑矩阵
		在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的 ... 
- leetcode 566 Reshape the Matrix 重塑矩阵
		参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ... 
- LeetCode Reshape the Matrix
		原题链接在这里:https://leetcode.com/problems/reshape-the-matrix/#/description 题目: In MATLAB, there is a ver ... 
- Leetcode  54:Spiral Matrix 螺旋矩阵
		54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ... 
- 【LeetCode】Spiral Matrix(螺旋矩阵)
		这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ... 
- [LeetCode] 73. Set Matrix Zeroes 矩阵赋零
		Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ... 
- [leetcode]54. Spiral Matrix螺旋矩阵
		Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ... 
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
		Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ... 
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
		Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ... 
随机推荐
- Javascript中几个看起来简单,却不一定会做的题
			Javascript作为前端开发必须掌握的一门语言,因为语言的灵活性,有些知识点看起来简单,在真正遇到的时候,却不一定会直接做出来,今天我们就一起来看看几道题目吧 题目1 var val = 'smt ... 
- RedHat/Fedora/Centos 下bash 自动补全命令
			本文转自:运维生存时间:http://www.ttlsa.com/linux/rhel- ... matically-function/ linuser :http://www.linuser.co ... 
- easyUi五个常用插件
			1.对话框(Dialog) 对话框(dialog)是一个特殊类型的窗口,它在顶部有一个工具栏,在底部有一个按钮栏.默认情况下,对话框(dialog)只有一个显示在头部右侧的关闭工具.用户可以配置对话框 ... 
- 用js来实现那些数据结构(数组篇01)
			在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ... 
- 201621123050 《Java程序设计》第6周学习总结
			1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 1.2 可选:使用常规方法总结其他上课内容. L ... 
- python 之反射
			通过字符串的形式导入模块 通过字符串的形式,去模块中寻找制定的函数,并执行getattr(模块名,函数名,默认值) 通过字符串的形式,去模块中设置东西setattr(模块名,函数名/变量名,lambd ... 
- 直方图均衡化及matlab实现
			在处理图像时,偶尔会碰到图像的灰度级别集中在某个小范围内的问题,这时候图像很难看清楚.比如下图: 它的灰度级别,我们利用一个直方图可以看出来(横坐标从0到255,表示灰度级别,纵坐标表示每个灰度级别的 ... 
- VS Code 常用命令记录
			1. 创建解决方案 例:dotnet new sln -o HelloWorld.Solutions 其中 -o 表示输出文件夹 2.创建类库.web.mvc.webapi等项目 例:dotnet n ... 
- JAVA_SE基础——32.this关键字调用本类的构造方法
			黑马程序员入学blog... 也算是学习笔记. 下面我们来看段代码: package day07; class Student{ int id; //身份证 String name; //名字 pub ... 
- GIT的安装及命令使用
			http://blog.jobbole.com/78960/ 因此:多人协作工作模式一般是这样的: 首先,可以试图用git push origin branch-name推送自己的修改. 如果推送失败 ... 
