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

Commitc663caa

Browse files
committed
src/bin/maximum-size-of-a-set-after-removals.rs
1 parentea344ec commitc663caa

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![allow(dead_code, unused, unused_variables, non_snake_case)]
2+
3+
fnmain(){}
4+
5+
structSolution;
6+
7+
implSolution{
8+
/// 1. 先去重
9+
/// 2. 移除都存在的数
10+
pubfnmaximum_set_size(nums1:Vec<i32>,nums2:Vec<i32>) ->i32{
11+
let l = nums1.len()asi32 /2;
12+
let n1 = nums1.into_iter().collect::<std::collections::HashSet<_>>();
13+
let n2 = nums2.into_iter().collect::<std::collections::HashSet<_>>();
14+
15+
letmut comm = n1.intersection(&n2).count()asi32;
16+
letmut result = n1.len()asi32 + n2.len()asi32 - comm;// 所有元素的数量
17+
18+
if n1.len()asi32 > l{
19+
let nm = comm.min(n1.len()asi32 - l);// 如果交集大于comm,则全部移除交集,否则,移除多余的n1的元素
20+
result -= n1.len()asi32 - nm - l;
21+
comm -= nm;
22+
}
23+
24+
if n2.len()asi32 > l{
25+
result -=(n2.len()asi32 - comm.min(n2.len()asi32 - l)) - l;
26+
}
27+
28+
result
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp