Skip to content

Commit 2541521

Browse files
committed
0004
1 parent 572aa2e commit 2541521

File tree

3 files changed

+59
-90
lines changed

3 files changed

+59
-90
lines changed
Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,64 @@
1-
package problem0004
1+
use std::cmp;
2+
use std::mem;
23

3-
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
4-
nums := combine(nums1, nums2)
5-
return medianOf(nums)
6-
}
4+
pub fn median_of_two_sorted_arrays (v1: Vec<i32>, v2: Vec<i32>) -> f32 {
75

8-
func combine(mis, njs []int) []int {
9-
lenMis, i := len(mis), 0
10-
lenNjs, j := len(njs), 0
11-
res := make([]int, lenMis+lenNjs)
12-
13-
for k := 0; k < lenMis+lenNjs; k++ {
14-
if i == lenMis ||
15-
(i < lenMis && j < lenNjs && mis[i] > njs[j]) {
16-
res[k] = njs[j]
17-
j++
18-
continue
19-
}
20-
21-
if j == lenNjs ||
22-
(i < lenMis && j < lenNjs && mis[i] <= njs[j]) {
23-
res[k] = mis[i]
24-
i++
25-
}
26-
}
27-
28-
return res
29-
}
6+
let mut vec1 = v1;
7+
let mut vec2 = v2;
8+
9+
if vec1.len() > vec2.len() {
10+
mem::swap(&mut vec1, &mut vec2);
11+
}
12+
13+
let len1 = vec1.len();
14+
let len2 = vec2.len();
15+
16+
let half_len = (len1 + len2 + 1) / 2;
17+
let mut index_min = 0;
18+
let mut index_max = len1;
3019

31-
func medianOf(nums []int) float64 {
32-
l := len(nums)
20+
while index_min <= index_max {
21+
let i = (index_min + index_max) / 2;
22+
let j = half_len - i;
3323

34-
if l == 0 {
35-
panic("切片的长度为0,无法求解中位数。")
36-
}
24+
if i < len1 && vec1[i] < vec2[j - 1] {
25+
index_min = i + 1;
26+
} else if i > 0 && vec1[i-1] > vec2[j] {
27+
index_max = i - 1;
28+
} else {
29+
let left_max;
30+
let right_min;
31+
if i == 0 {
32+
left_max = vec2[j - 1];
33+
} else if j == 0 {
34+
left_max = vec1[i-1];
35+
} else {
36+
left_max = cmp::max(vec1[i-1], vec2[j-1]);
37+
}
3738

38-
if l%2 == 0 {
39-
return float64(nums[l/2]+nums[l/2-1]) / 2.0
40-
}
39+
if (len1 + len2) % 2 == 1 {
40+
return left_max as f32;
41+
}
4142

42-
return float64(nums[l/2])
43+
if i == len1 {
44+
right_min = vec2[j];
45+
} else if j == len2 {
46+
right_min = vec1[i];
47+
} else {
48+
right_min = cmp::min(vec1[i], vec2[j]);
49+
}
50+
51+
return (left_max + right_min) as f32 / 2.0
52+
}
53+
}
54+
55+
return 0.0;
4356
}
57+
58+
fn main() {
59+
let vec1 = vec![9, 13, 17, 19];
60+
let vec2 = vec![2, 7, 9, 14, 15, 24, 28, 38];
61+
62+
let results = median_of_two_sorted_arrays(vec1, vec2);
63+
println!("{}", results);
64+
}
Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +0,0 @@
1-
package problem0004
2-
3-
import (
4-
"testing"
5-
6-
"github.com/stretchr/testify/assert"
7-
)
8-
9-
type para struct {
10-
one []int
11-
two []int
12-
}
13-
14-
type ans struct {
15-
one float64
16-
}
17-
18-
type question struct {
19-
p para
20-
a ans
21-
}
22-
23-
func Test_OK(t *testing.T) {
24-
ast := assert.New(t)
25-
26-
qs := []question{
27-
question{
28-
p: para{
29-
one: []int{1, 3},
30-
two: []int{2},
31-
},
32-
a: ans{
33-
one: 2,
34-
},
35-
},
36-
question{
37-
p: para{
38-
one: []int{1, 3},
39-
two: []int{2, 4},
40-
},
41-
a: ans{
42-
one: 2.5,
43-
},
44-
},
45-
}
46-
47-
for _, q := range qs {
48-
a, p := q.a, q.p
49-
ast.Equal(a.one, findMedianSortedArrays(p.one, p.two), "输入:%v", p)
50-
}
51-
52-
ast.Panics(func() { findMedianSortedArrays([]int{}, []int{}) }, "对空切片求中位数,却没有panic")
53-
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ leetcode in rust lang.
1515
|:-:|:-|:-:|:-:|:-:|
1616
|1|[Two Sum](./Algorithms/0001.two-sum)|39%|Easy||
1717
|2|[Add Two Numbers](./Algorithms/0002.add-two-numbers)|29%|Medium||
18-
|3|[Longest Substring Without Repeating Characters](./Algorithms/0003.longest-substring-without-repeating-characters)|25%|Medium||
18+
|3|[Longest Substring Without Repeating Characters](./Algorithms/0003.longest-substring-without-repeating-characters)|25%|Medium||
19+
|4|[Median of Two Sorted Arrays](./Algorithms/0004.median-of-two-sorted-arrays)|24%|Hard||

0 commit comments

Comments
 (0)