问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. 一文快速掌握华为云IPv6基础知识及使用指南

    随着5G.物联网等新兴技术领域的发展,IP空间需求巨大,IPv6成为万物互联的基础,势在必行:华为云作为IPv6成熟商用开拓者,针对金融.广电.媒资等不同行业推出IPv6解决方案,助力企业平滑升级到I ...

  2. 【面试题资源共享】一文总结最高频软件测试|sq|语句|思维发散|计算机基础|Linux|测试用例|接口测试|等技术面试题

    思维发散 1.一个球, -把尺子长度是球直径的2/3,怎样测出半径?2.四枚硬币,花面朝上,每次翻转三个,几次可以将四枚硬币变为字面朝上?3. U2合唱团在1 7分钟内赶到演唱会现场问题?4.小明一家 ...

  3. python学习03-使用动态ua

    在写爬虫的时候要使用到浏览器ua 分享一下今天学到的如何使用动态ua的进行爬取 1.简单的爬取网页信息 from urllib.request import urlopen #目标地址 url = & ...

  4. Python Ethical Hacking - Basic Concetion

    What is Hacking? Gaining unauthorized access. Hackers? 1.Black-hat Hackers 2.White-hat Hackers 3.Gre ...

  5. Python Ethical Hacking - Bypass HTTPS(2)

    Injecting Code in HTTPS Pages: #!/usr/bin/env python import re from netfilterqueue import NetfilterQ ...

  6. 虚拟化-SDDC软件定义数据中心

    一.什么是SDDC? SDDC依赖于虚拟化和云计算技术, SDDC的目标是虚拟化数据中心的一切物理资源,通过虚拟化的技术,构建一个由虚拟资源组成的资源池,不仅是对服务器进行虚拟化,还包括存储虚拟化和网 ...

  7. mysql 联合唯一

    CREATE TABLE `NewTable` ( `id` int NOT NULL , `name` varchar(255) NULL , `phone` varchar(255) NULL , ...

  8. JavaFX布局神器-SceneBuilder

    JavaFX允许开发使用FXML来设计和布局界面,跟Qt和Android的布局有点类似,JavaFX用SceneBuilder来设计和布局界面. SceneBuilder最新的下载地址:https:/ ...

  9. Python第一次实验

    ''' 计算 1.输入半径,输出面积和周长 2.输入面积,输出半径及周长 3.输入周长,输出半径及面积 ''' # # 1.输入半径,输出面积和周长 # from math import pi # # ...

  10. Python File isatty() 方法

    概述 isatty() 方法检测文件是否连接到一个终端设备,如果是返回 True,否则返回 False.高佣联盟 www.cgewang.com 语法 isatty() 方法语法如下: fileObj ...