File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
C#/LeetCodeResolves/LeetCodeResolves Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 3
3
<PropertyGroup >
4
4
<OutputType >Exe</OutputType >
5
5
<TargetFramework >netcoreapp3.1</TargetFramework >
6
+ <StartupObject >LeetCodeResolves.MedianOfTwoSortedArrays.Program</StartupObject >
6
7
</PropertyGroup >
7
8
8
9
</Project >
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments