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 ...
随机推荐
- 使用nuget过程中一些问题总结
更新System.Web.Http组件以及其相关依赖项使用以下命令更新: Update-Package Microsoft.AspNet.WebApi –reinstall 如果没有这个引用,则先添加 ...
- OCR
谷歌OCR光学字符识别窥探 - 简书 Tesseract OCR初探 利用Tesseract图片文字识别初探 _ TonyDeng's Blog Tesseract OCR(光学字符识别)教程 - C ...
- 1.Redis介绍以及安装
Redis介绍 Redis是一个开源的(BSD开源协议),内存数据结构存储,被用于作为数据库,缓存和消息代理. Redis支持如下数据结构: string(字符串) hashes(哈希) lists ...
- chromium之at_exit
// This class provides a facility similar to the CRT atexit(), except that // we control when the ca ...
- wireshark利用正则表达式过滤http协议中的jpg png zip等无用的数据包
主要工具:小度随身wifi热点 + wireshark抓包工具.(强烈不建议使用360的产品,非常垃圾,而且干扰代理#墙IP,搞得你不能***) 利用wireshark这个强大的协议分析利器.去分析某 ...
- jQuery的简单函数
1. jQuery函数的基本语法: $(document).ready(function(){ //代码块: }) 2.window.onload()和$(document).ready()的区分: ...
- 「PHP」简单工厂模式
引言 所属:创建型模式,常用设计模式之一 工厂模式分为:简单工厂模式.工厂方法模式.静态工厂模式.抽象工厂模式. 下面为简单工厂模式. 参考资料: <大话设计模式>程杰 模式概 ...
- [转]关于sdk更新Android SDK Tools 25.3.1版本后使用sdk manager闪退
昨天这两个manager还工作正常,今天更新了一下,发现不可用了,运行avd manager和sdk manager没反应,搜了好多文章,然后看到了下这篇文章<关于sdk更新Android SD ...
- php 计算两个文件的相对路径
<?php /** * 计算两个文件的相对路径 */ function relative_path($path1, $path2) { $arr1 = explode('/', dirname( ...
- 『Linux基础 - 3』 Linux文件目录介绍
Windows 和 Linux 文件系统区别 -- 结构 Windows 下的文件系统 - 在 Windows 下,打开 "计算机",我们看到的是一个个的驱动器盘符: - 每个驱动 ...