Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdff065c

Browse files
Create 4-Median-of-Two-Sorted-Arrays.java
1 parent9ebcd6b commitdff065c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*Brute-force solution (Linear time)*/
2+
3+
classSolution {
4+
publicdoublefindMedianSortedArrays(int[]nums1,int[]nums2) {
5+
intm =nums1.length;
6+
intn =nums2.length;
7+
int[]nums =newint[m+n];
8+
inti =0,j =0;
9+
intk =0;
10+
while (i<m &&j<n) {
11+
if (nums1[i]<nums2[j])nums[k++] =nums1[i++];
12+
elsenums[k++] =nums2[j++];
13+
}
14+
for (;i<m;i++)nums[k++] =nums1[i];
15+
for (;j<n;j++)nums[k++] =nums2[j];
16+
if ((m+n)%2 ==0) {
17+
return ((float)nums[(m+n-1)/2]+(float)nums[(m+n)/2])/(float)2;
18+
}elsereturn (float)nums[(m+n-1)/2];
19+
}
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp