-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp41.rs
32 lines (27 loc) · 894 Bytes
/
p41.rs
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
#[test]
fn test() {
use method1::first_missing_positive;
assert_eq!(first_missing_positive(vec![1, 2, 0]), 3);
assert_eq!(first_missing_positive(vec![3, 4, -1, 1]), 2);
assert_eq!(first_missing_positive(vec![7, 8, 9, 11, 12]), 1);
}
// 将nums放入哈希集中,然后从1开始枚举正整数,第一个不在哈希集中的正整数就是答案
// 空间复杂度是O(N),其实不满足要求
mod method1 {
pub fn first_missing_positive(nums: Vec<i32>) -> i32 {
use std::collections::HashSet;
let mut set: HashSet<i32> = HashSet::new();
for num in nums {
set.insert(num);
}
let mut missing: i32 = 0;
for num in 1..=i32::MAX {
if !set.contains(&num) {
missing = num;
break;
}
}
missing
}
}
// 还有其他方法,以后再说