705. 设计哈希集合 - 力扣(Leetcode)
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
| const base = 769
type MyHashSet struct { data []list.List }
func Constructor() MyHashSet { return MyHashSet{make([]list.List, base)} }
func (s *MyHashSet) hash(key int) int { return key % base }
func (s *MyHashSet) Add(key int) { if !s.Contains(key) { h := s.hash(key) s.data[h].PushBack(key) } }
func (s *MyHashSet) Remove(key int) { h := s.hash(key) for e := s.data[h].Front(); e != nil; e = e.Next() { if e.Value.(int) == key { s.data[h].Remove(e) } } }
func (s *MyHashSet) Contains(key int) bool { h := s.hash(key) for e := s.data[h].Front(); e != nil; e = e.Next() { if e.Value.(int) == key { return true } } return false }
|