This commit is contained in:
Electrominch
2022-10-06 00:46:00 +03:00
parent 062e03bdbf
commit cb2f99c3a8
21 changed files with 434 additions and 4 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_566._Reshape_the_Matrix</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,12 @@
namespace _566._Reshape_the_Matrix
{
internal class Program
{
static void Main(string[] args)
{
var res = new Solution().MatrixReshape(new int[][] { new int[] { 1,2 }, new int[] { 4, 5 } }, 2, 4);
foreach(var row in res)
Console.WriteLine(String.Join(" ", row));
}
}
}

View File

@@ -0,0 +1,27 @@
namespace _566._Reshape_the_Matrix;
public class Solution
{
public int[][] MatrixReshape(int[][] mat, int r, int c)
{
int[][] result = new int[r][];
for (int i = 0; i < result.Length; i++)
result[i] = new int[c];
int height = mat.Length;
int width = mat[0].Length;
int size = r * c;
if (size != height * width)
return mat;
for(int i = 0; i < size; i++)
{
int ySource = i / width;
int xSource = i % width;
int yTo = i / c;
int xTo = i % c;
result[yTo][xTo] = mat[ySource][xSource];
}
return result;
}
}