Day4
This commit is contained in:
11
566. Reshape the Matrix/566. Reshape the Matrix.csproj
Normal file
11
566. Reshape the Matrix/566. Reshape the Matrix.csproj
Normal 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>
|
||||
12
566. Reshape the Matrix/Program.cs
Normal file
12
566. Reshape the Matrix/Program.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
27
566. Reshape the Matrix/Solution.cs
Normal file
27
566. Reshape the Matrix/Solution.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user