• 力扣705-设计哈希集合


    原题 :https://leetcode-cn.com/problems/design-hashset/

    不使用任何内建的哈希表库设计一个哈希集合(HashSet)。

    实现 MyHashSet 类:

    void add(key) 向哈希集合中插入值 key 。
    bool contains(key) 返回哈希集合中是否存在这个值 key 。
    void remove(key) 将给定值 key 从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。



    个人理解

      同力扣706  https://www.cnblogs.com/wang102030/p/14533987.html

    class MyHashSet:

        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.buckets = 1009
            self.table = [ [] for _ in range(self.buckets) ]

        def hash(self,key):
            return key % self.buckets 

        def add(self, key: int) -> None:
            hashkey = self.hash(key)
            for item in  self.table[hashkey]:
                if item == key:
                    return
            self.table[hashkey].append(key)

        def remove(self, key: int) -> None:
            hashkey = self.hash(key)
            for i, item in enumerate(self.table[hashkey]):
                if item == key:
                    self.table[hashkey].pop(i)
                    return

        def contains(self, key: int) -> bool:
            """
            Returns true if this set contains the specified element
            """
            hashkey = self.hash(key)
            for item in self.table[hashkey]:
                if item == key:
                    return True
            return False
  • 相关阅读:
    php数组·的方法1-数组的操作
    第十一章:DOM扩展
    第十章:DOM
    hxq的库
    第八章:BOM
    第七章:函数表达式2
    第七章:函数表达式
    第五章:引用类型(一)-Object和Array
    舌尖上的程序猿
    计算矩阵运算的乘法次数
  • 原文地址:https://www.cnblogs.com/wang102030/p/14534130.html
Copyright © 2020-2023  润新知