-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0381_insert_delete_getrandom_o1_duplicates_allowed.rs
More file actions
110 lines (95 loc) · 3.11 KB
/
s0381_insert_delete_getrandom_o1_duplicates_allowed.rs
File metadata and controls
110 lines (95 loc) · 3.11 KB
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
#![allow(unused)]
use rand::{rngs::ThreadRng, thread_rng, Rng};
use std::collections::{HashMap, HashSet};
struct RandomizedCollection {
map: HashMap<i32, HashSet<usize>>,
list: Vec<i32>,
rng: ThreadRng,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl RandomizedCollection {
/** Initialize your data structure here. */
fn new() -> Self {
Self {
map: HashMap::new(),
list: Vec::new(),
rng: thread_rng(),
}
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
fn insert(&mut self, val: i32) -> bool {
let set = self.map.entry(val).or_insert(HashSet::new());
// insert duplicate or one val
set.insert(self.list.len());
self.list.push(val);
// check duplicate
set.len() == 1
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
fn remove(&mut self, val: i32) -> bool {
if !self.map.contains_key(&val) {
return false;
}
let id = *self.map.get(&val).unwrap().iter().next().unwrap();
self.map.get_mut(&val).unwrap().remove(&id);
let last = self.list[self.list.len() - 1];
// swap last val to list[id], and update last val id in map
self.list[id] = last;
self.map.get_mut(&last).unwrap().insert(id);
self.map
.get_mut(&last)
.unwrap()
.remove(&(self.list.len() - 1));
if self.map.get(&val).unwrap().len() == 0 {
self.map.remove(&val);
}
self.list.pop();
true
}
/** Get a random element from the set. */
fn get_random(&mut self) -> i32 {
// if list no elements , reutrn MIN mean a error occurs
if self.list.len() < 1 {
return std::i32::MIN;
}
let idx = self.rng.gen_range(0, self.list.len());
self.list[idx]
}
// for test
fn get(&self, val: i32) -> bool {
self.map.contains_key(&val)
}
}
/**
* Your RandomizedCollection object will be instantiated and called as such:
* let obj = RandomizedCollection::new();
* let ret_1: bool = obj.insert(val);
* let ret_2: bool = obj.remove(val);
* let ret_3: i32 = obj.get_random();
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_381() {
let mut obj = RandomizedCollection::new();
assert_eq!(obj.insert(1), true);
assert_eq!(obj.insert(1), false);
assert_eq!(obj.insert(2), true);
assert_eq!(obj.insert(1), false);
assert_eq!(obj.remove(1), true);
assert_eq!(obj.remove(1), true);
assert_eq!(obj.remove(2), true);
assert_eq!(obj.remove(2), false);
assert_eq!(obj.get_random(), 1);
assert_eq!(obj.remove(1), true);
assert_eq!(obj.get_random(), std::i32::MIN);
assert_eq!(obj.insert(2), true);
assert_eq!(obj.insert(3), true);
assert_eq!(obj.get(1), false);
//assert_eq!(obj.get_random(), 3);
}
}