20240414 Finished
This commit is contained in:
parent
b77e0295b2
commit
d427e7c1d8
917
Cargo.lock
generated
917
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
|
@ -9,7 +9,7 @@ reqwest = { version = "0.11.22", features = ["json"] }
|
|||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
serde_derive = "1.0"
|
||||
rand = "0.6.5"
|
||||
rand = "0.8.5"
|
||||
regex = "1.3.4"
|
||||
futures = { version = "0.3.11", features = ["thread-pool"] }
|
||||
surf = "1.0.3"
|
||||
|
|
|
@ -101,4 +101,5 @@ mod p121_best_time_to_buy_and_sell_stock;
|
|||
mod p122_best_time_to_buy_and_sell_stock_ii;
|
||||
mod p55_jump_game;
|
||||
mod p45_jump_game_ii;
|
||||
mod p274_h_index;
|
||||
mod p274_h_index;
|
||||
mod p380_insert_delete_getrandom_o1;
|
90
src/problem/p380_insert_delete_getrandom_o1.rs
Normal file
90
src/problem/p380_insert_delete_getrandom_o1.rs
Normal file
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* [380] Insert Delete GetRandom O(1)
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
use rand::{rngs::ThreadRng, Rng};
|
||||
use std::collections::HashMap;
|
||||
|
||||
struct RandomizedSet {
|
||||
array: Vec<i32>,
|
||||
pos_table: HashMap<i32, usize>,
|
||||
pos_now: usize,
|
||||
rng: ThreadRng
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl RandomizedSet {
|
||||
|
||||
fn new() -> Self {
|
||||
RandomizedSet {
|
||||
array: vec![0;200_001],
|
||||
pos_table: HashMap::new(),
|
||||
pos_now: 0,
|
||||
rng: rand::thread_rng()
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, val: i32) -> bool {
|
||||
if self.pos_table.contains_key(&val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.array[self.pos_now] = val;
|
||||
self.pos_table.insert(val, self.pos_now);
|
||||
self.pos_now += 1;
|
||||
true
|
||||
}
|
||||
|
||||
fn remove(&mut self, val: i32) -> bool {
|
||||
return match self.pos_table.get(&val) {
|
||||
Some(&pos) => {
|
||||
let last = self.array[self.pos_now - 1];
|
||||
self.pos_table.insert(last, pos);
|
||||
self.array[pos] = last;
|
||||
self.pos_now -= 1;
|
||||
self.pos_table.remove(&val);
|
||||
|
||||
true
|
||||
},
|
||||
None => false
|
||||
}
|
||||
}
|
||||
|
||||
fn get_random(&mut self) -> i32 {
|
||||
let pos = self.rng.gen_range(0..self.pos_now);
|
||||
self.array[pos]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RandomizedSet object will be instantiated and called as such:
|
||||
* let obj = RandomizedSet::new();
|
||||
* let ret_1: bool = obj.insert(val);
|
||||
* let ret_2: bool = obj.remove(val);
|
||||
* let ret_3: i32 = obj.get_random();
|
||||
*/
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_380() {
|
||||
let mut set = RandomizedSet::new();
|
||||
assert!(set.insert(0));
|
||||
assert!(set.insert(1));
|
||||
assert!(set.remove(0));
|
||||
assert!(set.insert(2));
|
||||
assert!(set.remove(1));
|
||||
assert_eq!(2, set.get_random());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user