题目

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.

You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

Example 1:

Input:
nums =
[[1,2],[3,4]]
r = 1, c = 4
Output:
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

Example 2:

Input:
nums =
[[1,2],[3,4]]
r = 2, c = 4
Output:
[[1,2],[3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

Note:

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

题目地址

讲解

二维数组,重新堆叠的问题。

java代码

class Solution {public int[][] matrixReshape(int[][] nums, int r, int c) {if(nums.length*nums[0].length != r*c){return nums;}else{int[][] result = new int[r][c];for(int i=0;i<r;i++){for(int j=0;j<c;j++){int sum = i*c+j;int row = sum/nums[0].length;int column = sum%nums[0].length;result[i][j] = nums[row][column];}} return result;}}
}

leetcode讲解--566. Reshape the Matrix相关推荐

  1. python矩阵reshape_[LeetCode Python3]566. Reshape the Matrix(重塑矩阵)

    在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的 ...

  2. Leetcode PHP题解--D53 566. Reshape the Matrix

    2019独角兽企业重金招聘Python工程师标准>>> D53 566. Reshape the Matrix 题目链接 566. Reshape the Matrix 题目分析 给 ...

  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

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

  5. leetcode 566. Reshape the Matrix | 566. 重塑矩阵(Java)

    题目 https://leetcode.com/problems/reshape-the-matrix/ 题解 考矩阵变换,简单题,不多说,直接上代码. class Solution {public ...

  6. leetCode题解之Reshape the Matrix

    1.题目描述 2.分析 使用了一个队列. 3.代码 1 vector<vector<int>> matrixReshape(vector<vector<int> ...

  7. C#LeetCode刷题之#566-重塑矩阵( Reshape the Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3720 访问. 在MATLAB中,有一个非常有用的函数 resha ...

  8. LeetCode 240. Search a 2D Matrix II

    LeetCode 240. Search a 2D Matrix II Solution1: 为什么把第74题的代码改都不用改的拿过来就可以AC,一脸懵逼啊... class Solution { p ...

  9. LeetCode 74. Search a 2D Matrix

    LeetCode 74. Search a 2D Matrix Solution1:我的答案 <剑指offer>原题 class Solution { public:bool search ...

最新文章

  1. clickhouse 基础知识
  2. 汇编中类似数组的寻址方式
  3. 前端工程师算法(一)
  4. c语言24点游戏流程图,C语言解24点游戏程序
  5. 搭建 Verilog 仿真环境
  6. java 程序流程控制知识点_JAVA基础知识点梳理三:流程控制语句
  7. mfc编程 孙鑫_孙鑫VC++视频教程笔记-(3)MFC程序框架的剖析 附1-SDI程序流程图
  8. ZZULIOJ 1052:数列求和4
  9. Ubuntu 终端不能输入中文,不能显示中文的解决办法
  10. 【原创】大端和小端字节序的细节
  11. 08cms中error_08cms_licens 故障
  12. C语言坐标打飞机,C语言实现打飞机小游戏
  13. Python数据库同步神器(一键同步)
  14. ASP.NET通过流方式导出EXCEL并且单元格换行
  15. 数学分析-换底公式证明
  16. 微信域名防红防屏蔽技术,微信域名总是被封要怎么解决
  17. 微积分精简版复习提纲
  18. taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,R2接口,oAuth2.0交易接口代码分享
  19. HTML简单电子日历的设计与实现
  20. RT3070 WIFI模块Android调试全记录

热门文章

  1. 找出重复的那个数字的异或算法
  2. asp创建mysql表_asp创建数据库表
  3. openssl算法 —— 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
  4. IDEA mybatis-generator 逆向工程
  5. 广告行业中常说的 CPC,CPM,CPD,CPT,CPA,CPS 等词的意思是什么?
  6. sublime text 的小细节设置,让你的代码更优美
  7. hadoop分布式搭建
  8. JavaScript核心参考手册.chm
  9. (一) 关于配置travis-ci持续集成python pytest测试的相关记录
  10. Core Java笔记 2.继承