Initial commit
This commit is contained in:
11
88. Merge Sorted Array/88. Merge Sorted Array.csproj
Normal file
11
88. Merge Sorted Array/88. Merge Sorted Array.csproj
Normal 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>
|
||||
14
88. Merge Sorted Array/Program.cs
Normal file
14
88. Merge Sorted Array/Program.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
42
88. Merge Sorted Array/Solution.cs
Normal file
42
88. Merge Sorted Array/Solution.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user