Compare commits

...

2 Commits

Author SHA1 Message Date
523f7a2609 feat: 705_design_hashset 2025-09-22 19:20:56 +08:00
d3fb915b95 feat: 706_design_hashmap 2025-09-22 19:20:28 +08:00
6 changed files with 130 additions and 0 deletions

7
705_design_hashset/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "design_hashset"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "design_hashset"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,47 @@
use std::hash::{DefaultHasher, Hash, Hasher};
pub struct MyHashSet {
capacity: usize,
buckets: Vec<Vec<i32>>,
}
impl MyHashSet {
pub fn new() -> Self {
const CAPACITY: usize = 4096;
Self {
capacity: CAPACITY,
buckets: vec![Vec::new(); CAPACITY],
}
}
pub fn add(&mut self, key: i32) {
let index = self.get_bucket_index(key);
let list = self.buckets.get_mut(index).unwrap();
if list.iter().find(|num| **num == key).is_none() {
list.push(key);
}
}
pub fn remove(&mut self, key: i32) {
let index = self.get_bucket_index(key);
let list = self.buckets.get_mut(index).unwrap();
if let Some(index_to_remove) = list.iter().position(|num| *num == key) {
list.remove(index_to_remove);
}
}
pub fn contains(&self, key: i32) -> bool {
let index = self.get_bucket_index(key);
let list = self.buckets.get(index).unwrap();
list.iter().find(|num| **num == key).is_some()
}
fn get_bucket_index(&self, key: impl Hash) -> usize {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
(hasher.finish() as usize) & (self.capacity - 1)
}
}

7
706_design_hashmap/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "design_hashmap"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "design_hashmap"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,57 @@
use std::hash::{DefaultHasher, Hash, Hasher};
pub struct MyHashMap {
capacity: usize,
buckets: Vec<Vec<(i32, i32)>>,
}
impl MyHashMap {
pub fn new() -> Self {
const CAPACITY: usize = 4096;
Self {
capacity: CAPACITY,
buckets: vec![Vec::new(); CAPACITY],
}
}
pub fn put(&mut self, key: i32, value: i32) {
let index = self.get_bucket_index(key);
let list = &mut self.buckets[index];
let node = list.iter_mut().find(|node| node.0 == key);
match node {
Some(node) => {
node.1 = value;
}
None => {
list.push((key, value));
}
}
}
pub fn get(&mut self, key: i32) -> i32 {
let index = self.get_bucket_index(key);
let list = &self.buckets[index];
let node = list.iter().find(|node| node.0 == key);
match node {
Some(node) => node.1,
None => -1,
}
}
pub fn remove(&mut self, key: i32) {
let index = self.get_bucket_index(key);
let list = &mut self.buckets[index];
if let Some(index_to_remove) = list.iter().position(|node| node.0 == key) {
list.remove(index_to_remove);
}
}
fn get_bucket_index(&mut self, key: i32) -> usize {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
hasher.finish() as usize & (self.capacity - 1)
}
}