Question

566. Reshape the Matrix

Solution

题目大意:给一个二维数组,将这个二维数组转换为r行c列

思路:构造一个r行c列的二维数组,遍历给出二给数组nums,合理转换原数组和目标数组的行列转换。

Java实现:

  1. public int[][] matrixReshape(int[][] nums, int r, int c) {
  2. int originalR = nums.length;
  3. int originalC = nums[0].length;
  4. if (originalR * originalC < r * c) {
  5. return nums;
  6. }
  7. int[][] retArr = new int[r][c];
  8. int count = -1;
  9. for (int i = 0; i < originalR; i++) {
  10. for (int j = 0; j < originalC; j++) {
  11. count++;
  12. int rIndex = count / c;
  13. int cIndex = count % c;
  14. // System.out.println(rIndex + "," + cIndex + "\t" + i + "," + j);
  15. retArr[rIndex][cIndex] = nums[i][j];
  16. // System.out.println(nums[i][j]);
  17. }
  18. }
  19. return retArr;
  20. }

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

  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 解题报告(Python)

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

  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. 【leetcode】566. Reshape the Matrix

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

  8. 566. Reshape the Matrix

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

  9. 566. Reshape the Matrix矩阵重排

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

随机推荐

  1. hackgame2018_签到

    hackgame2018_签到 进入题目得到如下提示 尝试提交 发现这个输入框长度做了限制我们将前端js修改一下 然后提交以下这样就发现了flag这题比较简单--

  2. C语言 | 栈区空间初探

    栈的定义 栈(stack)又名堆栈,堆栈是一个特定的存储区或寄存器,它的一端是固定的,另一端是浮动的 .对这个存储区存入的数据,是一种特殊的数据结构.所有的数据存入或取出,只能在浮动的一端(称栈顶)进 ...

  3. 【Visual Studio】VS 提示图标的含义

    一.前言 vs 中提示图标是什么意思 二.正文 信号图标 以下信号图标应用于所有原有的图标并指示它们的辅助功能. 图标 描述 <No Signal Icon> Public. 可从此组件中 ...

  4. SVG中的坐标系统和坐标变换

    视野和世界 2D绘图中很多人会有一个误区,就是我绘图的区域是一个矩形区域.无论新建一个画布还是创建了一个容器,心里都想象里面有一个矩形区域.其实,在SVG当中,矩形区域只是视野,是我们看到的部分.实际 ...

  5. Web Storage相关

    访问原文地址 概述 DOM存储的机制是通过存储字符串类型的键/值对,来提供一种安全的存取方式.这个附加功能的目标是提供一个全面的,可以用来创建交互式应用程序的方法(包括那些高级功能,例如可以离线工作一 ...

  6. CSS的inline、block与inline-block

    基本知识点 行内元素一般是内容的容器,而块级元素一般是其他容器的容器,行内元素适合显示具体内容,而块级元素适合做布局. 块级元素(block):独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默 ...

  7. prometheus之查询语言

    PromQL(Prometheus Query Language)是 Prometheus 自己开发的表达式语言,语言表现力很丰富,内置函数也很多.使用它可以对时序数据进行筛选和聚合. 一.PromQ ...

  8. PAT B1051 复数乘法

    输入样例: 2.3 3.5 5.2 0.4 输出样例: -8.68-8.23i 解题思路: 1.读入R1.P1.R2.P2. 2.A=(R1*R2)cos(P1+P2),B=(R1*R2)sin(P1 ...

  9. ubantu系统之 在当前文件夹打开终端

    直接安装一个软件包 "nautilus-open-terminal"终端输入:sudo apt-get install nautilus-open-terminal重启系统!

  10. hql常用查询语句

    // HQL: Hibernate Query Language.// 特点:// >> 1,与SQL相似,SQL中的语法基本上都可以直接使用.// >> 2,SQL查询的是表 ...