LeetCode 566. Reshape the Matrix (C++)
题目:
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.
分析:
给定一个矩阵,根据参数返回一个新的矩阵。
可以发现,只有给的r * c和原来矩阵行列乘积相同才可以将矩阵正确的变形,否则返回原来的矩阵。用f来确定原矩阵中元素的索引。直接两重循环将元素填进新矩阵中。
程序:
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int max = nums.size() * nums[].size();
        if(r == nums.size() || c == nums[].size() || max != r * c) return nums;
        vector<vector<int>> res(r, vector<int>(c, ));
        int f = ;
        for(int i = ; i < r; ++i)
            for(int j = ; j < c; ++j){
                res[i][j] = nums[f/nums[].size()][f%nums[].size()];
                f++;
            }
        return res;
    }
};
LeetCode 566. Reshape the Matrix (C++)的更多相关文章
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
		Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ... 
- 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 重塑矩阵
		参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ... 
- LeetCode -  566. Reshape the Matrix (C++) O(n)
		1. 题目大意 根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数.如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵.否则,重塑矩阵.其中两个矩阵中的数据顺序不变(先行后列). ... 
- 566. Reshape the Matrix - LeetCode
		Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ... 
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ... 
- [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
		原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ... 
随机推荐
- BurpSuite+SQLmap的一种另类扫描
			过年之后就忙的团团转.三月开始可以轻松一些,抽空写写最近瞎折腾的东西,本文只是描述工具的一种使用方法,无技术含量.(PS:这种做法,网上也有很多教程,本文只为记录). 由于公司使用的电脑都是win10 ... 
- P3133 [USACO16JAN]无线电联系Radio Contact
			题目描述 Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! ... 
- socket编程小问题:地址已经被使用——Address already in use
			很多socket编程的初学者可能会遇到这样的问题:如果先ctrl+c结束服务器端程序的话,再次启动服务器就会出现Address already in use这个错误,或者你的程序在正常关闭服务器端so ... 
- SharePoint自动初始化网站列表
			1,由于目前的SharePoint网站需要部署到多个服务器上,每个网站的内容都不一样,所以使用备份还原是不可以的.常用的方式便是将列表导出为列表模版,然后将列表模版复制到服务器上,根据列表模版创建列表 ... 
- 三层架构搭建(asp.net mvc + ef)
			第一次写博客,想了半天先从简单的三层架构开始吧,希望能帮助到你! 简单介绍一下三层架构, 三层架构从上到下分:表现层(UI),业务逻辑层(BLL),数据访问层(DAL)再加上数据模型(Model),用 ... 
- block本质探寻四之copy
			说明: <1>阅读本文,最好阅读之前的block文章加以理解: <2>本文内容:三种block类型的copy情况(MRC).是否深拷贝.错误copy: 一.MRC模式下,三种b ... 
- 使用jquery获取url上的参数(笔记)
			使用jquery获取url上的参数(笔记) 一.做作业时经常要获取url上的参数 1.当url上有多个参数时 从互联网找到了一个方法 (function ($) { $.getUrlParam = f ... 
- spark上的一些常用命令(一)
			1. 加速跑 spark-sql --name uername --num-executors --driver-memory 8G --executor-memory 8G 2. 上传数据 建表 ) ... 
- jslint
			auto execution/self execution/ Immediate function http://www.jslint.com/ (function () { 'use strict' ... 
- 1 CRM需求分析,数据库表,录入数据
			1.需求分析 CRM客户关系管理软件---> 学员管理 用户:企业内部用户 用户量: 业务场景: 2.数据库表设计 1 .表之间的对应关系 from django.db import model ... 
