-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
44 lines (42 loc) · 987 Bytes
/
Solution.cs
File metadata and controls
44 lines (42 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class Solution
{
public double FindMedianSortedArrays(int[] nums1, int[] nums2)
{
int n = nums1.Length + nums2.Length;
int r = 0;
int l = 0;
int[] res = [0, 0];
for (int i = 0; i <= n / 2; i++)
{
res[0] = res[1];
if (l < nums1.Length && r < nums2.Length)
{
if (nums1[l] <= nums2[r])
{
res[1] = nums1[l];
l++;
}
else
{
res[1] = nums2[r];
r++;
}
}
else if (l < nums1.Length)
{
res[1] = nums1[l];
l++;
}
else
{
res[1] = nums2[r];
r++;
}
}
if (n % 2 == 0)
{
return (res[0] + res[1]) / 2.0;
}
return res[1];
}
}