zoukankan      html  css  js  c++  java
  • 力扣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
  • 相关阅读:
    工作感悟(一)
    laydate组件选择时间段的判断
    Win10下免安装版JDK8环境变量配置
    IDEA中lombok插件的安装
    解决加载WEB页面时,由于JS文件引用过多影响页面打开速度的问题
    Windows环境下的MYSQL5.7配置文件定位
    MYSQL使用source命令,导入SQL文件
    MYSQL5.7生成列简介及创建
    MYSQL慢查询优化方法及优化原则
    批量提取文件夹下所有目录及文件名称
  • 原文地址:https://www.cnblogs.com/wang102030/p/14534130.html
Copyright © 2011-2022 走看看