问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3720 访问。

在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。

给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的行数和列数。

重构后的矩阵需要将原始矩阵的所有元素以相同的行遍历顺序填充。

如果具有给定参数的reshape操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。

输入: 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
输出: 
[[1,2,3,4]]
解释:
行遍历nums的结果是 [1,2,3,4]。新的矩阵是 1 * 4 矩阵, 用之前的元素值一行一行填充新矩阵。

输入: 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
输出: 
[[1,2],
 [3,4]]
解释:
没有办法将 2 * 2 矩阵转化为 2 * 4 矩阵。 所以输出原矩阵。

注意:

给定矩阵的宽和高范围在 [1, 100]。
给定的 r 和 c 都是正数。


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.

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.

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:

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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3720 访问。

public class Program {public static void Main(string[] args) {int[,] nums = null;nums = new int[2, 2] { { 1, 2 }, { 3, 4 } };var res = MatrixReshape(nums, 1, 4);ShowArray(res);Console.ReadKey();}private static void ShowArray(int[,] array) {foreach(var num in array) {Console.Write($"{num} ");}Console.WriteLine();}private static int[,] MatrixReshape(int[,] nums, int r, int c) {if((r * c) != nums.Length) return nums;var result = new int[r, c];var count = -1;foreach(var num in nums) {result[++count / c, count % c] = num;}return result;}}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3720 访问。

1 2 3 4

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#566-重塑矩阵( Reshape the Matrix)相关推荐

  1. C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...

  2. C#LeetCode刷题之#867-转置矩阵(Transpose Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3756 访问. 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的 ...

  3. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

  4. LeetCode刷题(40)--Search a 2D Matrix

    已经排好序的矩阵,搜索一个元素,二分法 class Solution(object):def searchMatrix(self, matrix, target):""" ...

  5. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  6. ​LeetCode刷题实战584:寻找用户推荐人

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  7. ​LeetCode刷题实战577:员工奖金

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  8. ​LeetCode刷题实战568:最大休假天数

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  9. ​LeetCode刷题实战81:搜索旋转排序数组 II

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

最新文章

  1. 我在不炎熱也不抑鬱的秋天,依然不抽煙
  2. H5支持度检测常用模式(仅干货)
  3. LBS将使品牌口碑更真实
  4. 新手学测试----Unit Test(单元测试)
  5. 温州大学《机器学习》课程代码(四)朴素贝叶斯
  6. Grafana+Prometheus系统监控之MySql
  7. boost.asio系列——io_service
  8. 最稳妥的服务器阵列方案:RAID5+热备盘
  9. ExtJs六(ExtJs Mvc首页展示)
  10. 指定输出路径_新版Creo输入输出配置不用愁,果断收藏本文就对了
  11. python教程视频下载-python怎么下载视频
  12. spring-aop源码分析
  13. 霍夫线变换,霍夫圆变换
  14. 解码.NET 2.0配置之谜(一)
  15. 红米AC2100刷固件心得
  16. 首届技术播客月开播在即
  17. html显示vbs变量,VBS 读取 对象某属性已连接的变量的变量名
  18. 概要设计的过程和任务
  19. 常用搜索方法(部分)
  20. 人生不该有如此压力,来吃下这口缓解焦虑的良药[50P]

热门文章

  1. Linux(ubuntu)更换内核方法
  2. 【AI视野·今日Robot 机器人论文速览 第二十八期】Wed, 1 Dec 2021
  3. java中import两种导入类型比较
  4. 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
  5. 四窗口卖票 自己的票
  6. 常见运行时异常 java 114982568
  7. 草稿 图片盒子定时器模式窗口
  8. django-索引1909
  9. spring学习(四) ———— 整合web项目(SSH)
  10. 在两个Silverlight应用间数据通信(包括与Flash通信)