LeetCode - 566. Reshape the Matrix (C++) O(n)
1. 题目大意
根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数。如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵。否则,重塑矩阵。其中两个矩阵中的数据顺序不变(先行后列)。
2. 思路
由于矩阵中数据顺序不变,因此我们考虑按顺序做。原矩阵中的第i行第j列(从0开始)的数据可以记为第k个数,其中k=i*(原矩阵中的列数)+j。对应的是新产生的矩阵中的第k/c行,k%c列的元素。一一赋值。这题的关键是要小心数组的边界,正确找到对应的位置。
3. 代码
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int total = r*c, originr = nums.size(), originc = nums[0].size();
if(total != (originr*originc)) return nums;
vector<vector <int> > res(r ,vector<int>(c));
for(int k = 0; k < total; k++) res[k / c][k % c] = nums[k / originc][k % originc];
return res;
}
};
LeetCode - 566. Reshape the Matrix (C++) O(n)的更多相关文章
- 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 (C++)
题目: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a n ...
- leetcode 566 Reshape the Matrix 重塑矩阵
参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...
- 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 ...
随机推荐
- Java反射机制--是什么,为什么,怎么用。
往往当我们面对一项新的知识时,我们往往需要知道三个方面,它是什么,它能做什么,它比原有知识强在哪里,我们该怎么使用它.当你能够解决这些问题时,便意味着你已经对这项知识入门了. 一.是什么 Java R ...
- 环境配置之 Debug 和 Release - iOS
便于开发.打包中在不同环境(测试.生产)间属性的切换更加方便便捷流畅,故创建设置此方式方法,希望对大家能有所帮助. 首先,创建 Configurations Setting File(.xcconfi ...
- CentOS 6.8安装Ceph
机器规划 IP 主机名 角色 10.101.0.1 ceph01 mon admin mds 10.101.0.2 ceph02 ods 10.101.0.3 ceph03 ods 10.101.0. ...
- 路由(二) router-link的使用
main.js import Vue from 'vue'import App from './App'import VueRouter from 'vue-router'import footer ...
- tornado用户指引(四)------tornado协程使用和原理(三)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/happyAnger6/article/details/51291221几种常用的协程方式: 1.回调 ...
- tp3.2源码解析——入口文件
如果有人读这篇文章并跟着做的话,希望你能使用支持函数跳转的编辑器,还要善用var_dump和exit,对着源码去调试着看.跟着入口文件读,执行到哪里你看到哪里,对于那些不能一眼看出来的配置,则要记录下 ...
- Oracle_11g桌面版 中解决被锁定的scott 教学数据库的方法
Oracle 11g中修改被锁定的用户:scott 在安装完Oracle10g和创建完oracle数据库之后,想用数据库自带的用户scott登录,看看连接是否成功. 在cmd命令中,用“sqlplus ...
- HDFS原理
1 . NameNode 概述 a. NameNode 是 HDFS 的核心. b. NameNode 也称为 Master. c. NameNode 仅存储 HDFS 的元数据:文件系统中所有文件的 ...
- (杭电2053)A + B Again(转换说明符)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): ...
- c语言指针的指针
c语言在函数传递时常常使用如下的形式. void get(int **p) 对于这个形式,我想过为什么不能够使用 *p 作为形参呢.下面我们看一下代码和执行结果 void get(int **p) { ...