Question

566. Reshape the Matrix

Solution

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

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

Java实现:

public int[][] matrixReshape(int[][] nums, int r, int c) {
int originalR = nums.length;
int originalC = nums[0].length;
if (originalR * originalC < r * c) {
return nums;
}
int[][] retArr = new int[r][c];
int count = -1;
for (int i = 0; i < originalR; i++) {
for (int j = 0; j < originalC; j++) {
count++;
int rIndex = count / c;
int cIndex = count % c;
// System.out.println(rIndex + "," + cIndex + "\t" + i + "," + j);
retArr[rIndex][cIndex] = nums[i][j];
// System.out.println(nums[i][j]);
}
}
return retArr;
}

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. 决策树3:基尼指数--Gini index(CART)

    既能做分类,又能做回归.分类:基尼值作为节点分类依据.回归:最小方差作为节点的依据. 节点越不纯,基尼值越大,熵值越大 pi表示在信息熵部分中有介绍,如下图中介绍 方差越小越好. 选择最小的那个0.3 ...

  2. [转载] Link prefetch

    本来想翻译的但是有人翻译了,还是转过来吧.原文<HTML5 Link Prefetching>,译文<使用HTML5的页面资源预加载(Link prefetch)功能加速你的页面加载 ...

  3. Unity用Input.touches实现手机端多点触控

    多点触控的方法,两边的触控互不干扰: 主要采用Input.touches的相关属性进行操作: 而采用IPointerDrag接口会造成两个drag的相互干扰: 代码如下: using System.C ...

  4. java中异常到底有什么用?举例

    异常的意义:马克-to-win:通过上面的例子,我们看出通过引入异常这种技术,即使出现不测(用户把0赋给除数),也可以让程序不崩溃,还能继续优雅 的运行.那,这种技术有用,值得学.马克-to-win: ...

  5. java中为什么接口中的属性都默认为static和final?

    1)为什么接口中的属性都默认为static和final?Sun公司当初为什么要把java的接口设计发明成这样?[新手可忽略不影响继续学习]答:马克-to-win:接口中如果可能定义非final的变量的 ...

  6. TextView显示html样式的文字

    项目需求: TextView显示一段文字,格式为:白雪公主(姓名,字数不确定)向您发来了2(消息个数,不确定)条消息 这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色. 一开始我 ...

  7. box-shadow 阴影的高级用法,多个阴影叠加

    box-shadow的这些用法你知道吗? $shadowH: ''; @for $i from 1 through 12 { $shadowH: #{$shadowH}, 0 ($i * 30px) ...

  8. vs技巧 - 调试asp.net core源码

    学习asp.net core的方式除了看官方文档,看源码是也是一种很好的方式.本文介绍一种方法,简单配置vs,无需第三方插件就可以将asp.net core的源码链接自己的项目,随时穿梭于core的源 ...

  9. 【深度学习 论文篇 01-1 】AlexNet论文翻译

    前言:本文是我对照原论文逐字逐句翻译而来,英文水平有限,不影响阅读即可.翻译论文的确能很大程度加深我们对文章的理解,但太过耗时,不建议采用.我翻译的另一个目的就是想重拾英文,所以就硬着头皮啃了.本文只 ...

  10. MFC---视图和窗口

    视类窗口 视类窗口是指程序运行后,显示信息的那一部分.对应的类是CTestOneView(TestOne表示项目名称)类,CTestOneView类是派生于CView类,而CView类又派生于CWnd ...