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)的更多相关文章

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

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

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

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

  3. LeetCode 566 Reshape the Matrix 解题报告

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

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

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

  5. leetcode 566 Reshape the Matrix 重塑矩阵

    参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector&l ...

  6. 566. Reshape the Matrix - LeetCode

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

  7. 【LeetCode】566. Reshape the Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...

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

  9. 【leetcode】566. Reshape the Matrix

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

随机推荐

  1. java SSM 框架 代码生成器 websocket即时通讯 shiro redis

    1.   权限管理:点开二级菜单进入三级菜单显示 角色(基础权限)和按钮权限      角色(基础权限): 分角色组和角色,独立分配菜单权限和增删改查权限.      按钮权限: 给角色分配按钮权限. ...

  2. STL笔记

    STL的基本概念: 1-容器:是可容纳各种类型的数据结构,是 类模板. 2-迭代器:是用于依次存放容器中的元素,类似指针. 3-算法: 是用于操作容器中元素的 函数模板. sort() 用来对 vec ...

  3. MySQL 5.7基于GTID的主从复制

            since i've broken down the replication enviornment by "reset master;" yesterday.th ...

  4. The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.报该错误的一种原因。

    今天发现某个action返回404. HTTP Status 404 – Not Found Type Status Report Message /xxx.action Description Th ...

  5.  linux命令sed与awk是干什么用的,怎么用?

    非常强大的文本操纵工具,sed,awk,grep 这个三个命令 都是操作文本文件的unix系统有几个非常命令的特点:1. 对于内核而言,unix文件都是字节序列.io设备也是文件.2. 至于文件的含义 ...

  6. 基于CLGeocoder - 地理编码

    iOS中CoreLocatio框架中的CLGeocoder为我们提供了地理编码方法: 首先需要导入框架 #import <CoreLocation/CoreLocation.h> 地理编码 ...

  7. 微信小程序上滑加载更多

    onReachBottom: function () { var that = this var limit = that.data.limit var count = that.data.count ...

  8. python3 练习题100例 (四)

    题目四:输入某年某月某日,判断这一天是这一年的第几天? #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目四:输入 ...

  9. 中国大学MOOC-C程序设计(浙大翁恺)—— 单词长度

    题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束.你要输出这行文本中每个单词的长度.这里的单词与语言无关,可以包括各种符号,比如“it's”算一个单词,长度为4.注意,行中 ...

  10. C语言实验报告(五) 用自定义函数求2~n之间的素数

    #include<stdio.h>#include <math.h>int main(){  int i,n;  printf("input n:");  ...