566. Reshape the Matrix
来源:互联网 发布:巴克利生涯数据 编辑:程序博客网 时间:2024/06/05 04:36
题目的要求是:给定一个矩阵,根据给出的r跟c,即行数跟列数,去重塑矩阵,即返回一个行数为r,列数为c的矩阵。其实解题思路很简单,因为不管矩阵怎么排列,元素的总个数是不变的,我们只需要重新计算每个元素的位置即可,就是计算出每个元素在新矩阵中的下标。完整的代码如下:
class Solution {public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { int row = nums.size(); int col = nums[0].size(); if (r * c != row * col) return nums; vector<vector <int>> shape(r, vector<int>(c, 0)); for (int i = 0; i < row * col; i++) { shape[i / c][i % c] = nums[i / col][i % col]; } return shape; }};
阅读全文
0 0
- 566. Reshape the Matrix
- 566.Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- JAVA与XML转换
- 常用的命令
- Fluid Mask 3.3.16 在 mac OS X Sierra (10.12) 和 High Sierra (10.13)系统中无法启动和不能作为PhotoShop 插件使用问题修复
- Maven之(二)Maven生命周期
- 疯狂Activiti6连载(4)流程引擎数据源配置
- 566. Reshape the Matrix
- 开源数据库迁移工具 – Flyway
- find命令 -- 基于时间查找
- 怎么找回回收站已经删除的文件
- Maven之(三)Maven插件
- 原型模式
- 成为java高级程序员需要掌握哪些
- selenium自动化测试面试题
- 全新的松松软文平台上线公告