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
  • 相关阅读:
    Big Number
    Who will be punished
    find your present (2)
    Being a Good Boy in Spring Festival
    day4__列表的初识(列表的创建、增删改查、元组、range)
    day3、基础___(基础数字类型、字符串索引与切片、str常用操作方法)
    day2、基础__(while循环、格式化输出、运算符、初始编码)
    day1: 基础 __ (变量、常量、注释、数据类型、input、 if)
    十八、FTP配置
    十七、交换机配置管理IP和telnet登陆设置
  • 原文地址:https://www.cnblogs.com/wang102030/p/14534130.html
Copyright © 2011-2022 走看看