Initial commit

This commit is contained in:
Electrominch
2022-10-04 13:40:57 +03:00
commit c9916dabad
34 changed files with 1289 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,14 @@
namespace _88._Merge_Sorted_Array
{
internal class Program
{
static void Main(string[] args)
{
int[] n1 = new int[] { 1, 2, 3, 0, 0, 0 };
int[] n2 = new int[] { 2,5,6 };
Solution solution = new Solution();
solution.Merge(n1, 3, n2, n2.Length);
Console.WriteLine(String.Join(" ", n1));
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace _88._Merge_Sorted_Array;
public class Solution
{
public void Merge(int[] nums1, int m, int[] a2, int n)
{
int[] a1 = new int[m];
for(int i = 0; i < m; i++)
a1[i] = nums1[i];
int resIndex = 0;
int a1Index = 0, a2Index = 0;
while (resIndex < nums1.Length)
{
int? n1 = a1Index < a1.Length ? a1[a1Index] : null;
int? n2 = a2Index < a2.Length ? a2[a2Index] : null;
int toWrite = 0;
if (n1 != null && n2 != null)
{
toWrite = Math.Min(n1.Value,n2.Value);
if (n1 < n2)
a1Index++;
else
a2Index++;
}
else if(n1!=null)
{
toWrite = n1.Value;
a1Index++;
}
else if(n2!=null)
{
toWrite = n2.Value;
a2Index++;
}
nums1[resIndex++] = toWrite;
}
}
}