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.

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:

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

我的照葫芦画瓢代码:

精巧想法是: res[i / c][i % c] = A[i / cc][i % cc]; 老服气了!

副产品: vector<vector<int>>vec(m,vector<int>(n,0));

m*n的二维vector,所有元素为0

vector<vector<int>> matrixReshape(vector<vector<int>>& A, int r, int c) {
int rr = A.size(), cc = A[0].size();
if (rr * cc != r * c) return A; vector<vector<int>> res(r, vector<int>(c, 0));
for (int i = 0; i < r * c; i++)
res[i / c][i % c] = A[i / cc][i % cc];
return res;
}

566. Reshape the Matrix的更多相关文章

  1. Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)

    Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...

  2. 566. Reshape the Matrix - LeetCode

    Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums, ...

  3. LeetCode 566. Reshape the Matrix (重塑矩阵)

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  4. LeetCode 566 Reshape the Matrix 解题报告

    题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...

  5. [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 ...

  6. LeetCode 566. Reshape the Matrix (C++)

    题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...

  7. 566. Reshape the Matrix矩阵重排

    [抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...

  8. 【leetcode】566. Reshape the Matrix

    原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...

  9. [Array] 566. Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

随机推荐

  1. Groovy入门(2-2)Groovy的eclipse插件安装

    1.安装eclipse插件 启动eclipse,点击help -> Install New Software... 在弹出的窗口中点击:Add... Groovy插件的地址:http://dis ...

  2. python 模拟浏览器登陆coursera

    import requests import random import string def randomString(length): return ''.join(random.choice(s ...

  3. python/Django(增、删、改、查)操作

    python/Django(增.删.改.查)操作 我们要通过pycharm中的Django模块连接MySQL数据库进行对数据的操作. 一.创建Django项目(每创建一个项目都要进行以下设置) 1.如 ...

  4. SQL server2017的操作(练习题)

    题目: 假设有教材管理数据库BM,包括3个基本表: 教材(书号,书名,作者,出版社) B(Bno, Bname, Author, pub) 班级(班号,专业,所在系,人数) C(Cno, Spe, D ...

  5. git的理论基础

    GIT是目前世界上最先进最牛逼的分布式版本控制系统git维护的三棵树分别是工作区域.暂存区域.git仓库工作区域:就是你平时存放项目代码的地方暂存区域:用于临时存放你的改动,事实上它只是一个文件,保存 ...

  6. 判断字符串的后缀.endswith()

    可以用str.endswith('.jpg')来判断字符串是否以jpg结尾,返回True或者False

  7. window下编辑了脚本文件linux报如下错误

    首先vi :set ff 查看文件类型 接着 下载dos2unix  root用户下yum -y install dos2unix 然后 dos2unix 文件.sh 转换格式  接着在正常启动即可

  8. mysql索引类型和索引方法

    索引类型 mysql索引类型normal,unique,full text的区别是什么? normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号 ...

  9. angualar2——八大组成

    Angular2 模块 理解: Angular 应用是模块化的,并且 Angular 有自己的模块系统,它被称为 Angular 模块或 NgModules. 组件 组件是一个项目主干,一个模块由多个 ...

  10. webpack构建react项目(一)

    前言 下面是我们使用到技术栈: webpack + react + redux + react-router + react-thunk + ES6 + .... 注意事项: 建议使用npm5.X 或 ...