-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004_Median_of_Two_Sorted_Arrays.java
114 lines (100 loc) · 3.06 KB
/
004_Median_of_Two_Sorted_Arrays.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class Solution {
// example in leetcode book
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int p1 = 0, p2 = 0, pos = 0;
int ls1 = nums1.length, ls2 = nums2.length;
int[] all_nums = new int[ls1+ls2];
double median = 0.0;
while (p1 < ls1 && p2 < ls2){
if (nums1[p1] <= nums2[p2])
all_nums[pos++] = nums1[p1++];
else
all_nums[pos++] = nums2[p2++];
}
while (p1 < ls1)
all_nums[pos++] = nums1[p1++];
while (p2 < ls2)
all_nums[pos++] = nums2[p2++];
if ((ls1 + ls2) % 2 == 1)
median = all_nums[(ls1 + ls2) / 2];
else
median = (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2.0;
return median;
}
}
****************************************************************************************************
class Solution {
public double findMedianSortedArrays(int[] num1, int[] num2) {
int length = num1.length + num2.length;
int mid1 = 0;
int mid2 = 0;
int index = 0;
int i = 0;
int j = 0;
while (index <= length/2) {
while (index <= length/2 && i < num1.length && j < num2.length) {
if (num1[i] < num2[j]) {
mid1 = mid2;
mid2 = num1[i];
i++;
} else {
mid1 = mid2;
mid2 = num2[j];
j++;
}
index++;
}
while(index <= length/2 && i < num1.length) {
mid1 = mid2;
mid2 = num1[i];
i++;
index++;
}
while(index <= length/2 && j < num2.length) {
mid1 = mid2;
mid2 = num2[j];
j++;
index++;
}
}
double median = 0d;
if (length % 2 == 0) {
median = (mid1 + mid2) / 2.0;
} else {
median = mid2;
}
return median;
}
}
************************************************************************************************
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int l1 = nums1.length;
int l2 = nums2.length;
int l3 = l1 + l2;
int [] merge = new int [l3];
for (int i = l3 - 1; i >= 0; i--) {
while (l1 != 0 && l2 != 0) {
if (nums1[l1 - 1] >= nums2[l2 - 1]) {
merge[i--] = nums1[--l1];
} else {
merge[i--] = nums2[--l2];
}
}
while (l1 != 0) {
merge[i--] = nums1[--l1];
}
while (l2 != 0) {
merge[i--] = nums2[--l2];
}
}
double median;
int mid = l3 / 2;
if (l3%2 > 0) {
median = merge[mid];
} else {
median = (double) (merge[mid - 1] + merge[mid]) / 2;
}
return median;
}
}