问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. LeetCode 566. 重塑矩阵(Reshape the Matrix)

    566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...

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

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

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

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

  4. leetcode刷题记录——数组与矩阵

    @ 目录 283. 移动零 566. 重塑矩阵 485. 最大连续1的个数 240. 搜索二维矩阵 II 378. 有序矩阵中第K小的元素 645. 错误的集合 287. 寻找重复数 667. 优美的 ...

  5. [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix

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

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

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

  7. C#LeetCode刷题-数组

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

  8. Java实现 LeetCode 566 重塑矩阵(遍历矩阵)

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

  9. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

随机推荐

  1. CSS文本溢出,显示省略号(...)

    方式1:这种方式就是必须要跟width搭配使用,当你使用的display:flex; 的时候可能会影响样式 width: 200px; overflow: hidden; /*超出部分隐藏*/ tex ...

  2. CentOS7 源码编译安装Nginx

    源码编译安装nginx     1.下载nginx源码包(这里以nginx-1.18.0为例) wget http://nginx.org/download/nginx-1.18.0.tar.gz 2 ...

  3. 从连接器组件看Tomcat的线程模型——BIO模式

    在高版本的Tomcat中,默认的模式都是使用NIO模式,在Tomcat 9中,BIO模式的实现Http11Protocol甚至都已经被删除了.但是了解BIO的工作机制以及其优缺点对学习其他模式有有帮助 ...

  4. 【测试工具】这些APP实用测试工具,不知道你就out了!

    本期,我将给大家介绍14款实用的测试工具,希望能够帮到大家!(建议收藏) UI自动化测试工具 1. uiautomator2 Github地址:https://github.com/openatx/u ...

  5. DJANGO-天天生鲜项目从0到1-002-用户模块-注册

    本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...

  6. Cobbler 部署

    环境介绍 Cobbler 操作系统: Centos-7.2-x86_64 Cobbler服务器地址: 10.90.0.10 部署 Cobbler 安装 Centos epel 原 [root@node ...

  7. Jenkins部署jmx脚本

      针对jenkins+jmeter做接口自动化,jmeter完成测试脚本录入和撰写,而jenkins负责持续集成和报告收集.那么从零实现jenkins可以从以下角度实现 首先需要安装jenkins ...

  8. 使用MacOS直播

    参考链接:https://www.jianshu.com/p/94f42a793a7e 参考链接:https://blog.dreamtobe.cn/live_guideline/ 所需软件  密码: ...

  9. 巩固复习(Hany驿站原创)_python的礼物

    Python编程语言简介 https://www.cnblogs.com/hany-postq473111315/p/12256134.html Python环境搭建及中文编码 https://www ...

  10. 给定两个列表,转换为 DataFrame 类型

    import pandas as pd def get_data(): q1 = [] q2 = [] p1 = input("list 1:") p2 = input(" ...