Skip to content

Commit cd1fef3

Browse files
committed
median of two sorted arrays solution
1 parent ef12491 commit cd1fef3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

C#/LeetCodeResolves/LeetCodeResolves/LeetCodeResolves.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<StartupObject>LeetCodeResolves.MedianOfTwoSortedArrays.Program</StartupObject>
67
</PropertyGroup>
78

89
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace LeetCodeResolves.MedianOfTwoSortedArrays
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var num1 = new int[] { 1, 1 };
12+
var num2 = new int[] { 1, 2 };
13+
14+
var res = FindMedianSortedArrays(num1, num2);
15+
16+
Console.ReadLine();
17+
}
18+
19+
private static double FindMedianSortedArrays(int[] nums1, int[] nums2)
20+
{
21+
var mergedArray = nums1.Concat(nums2).ToArray();
22+
Array.Sort(mergedArray);
23+
24+
return GetMedian(mergedArray);
25+
}
26+
27+
private static double GetMedian(int[] arr)
28+
{
29+
int idx = arr.Count() / 2;
30+
if (arr.Length % 2 == 0)
31+
{
32+
return (double)((double)(arr[idx] + arr[idx - 1]) / 2);
33+
}
34+
else
35+
{
36+
return arr[idx];
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)