作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/design-hashset/description/

题目描述

Design a HashSet without using any built-in hash table libraries.

To be specific, your design should include these two functions:

  • add(value): Insert a value into the HashSet.
  • contains(value) : Return whether the value exists in the HashSet or not.
  • remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.

Example:

MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // returns true
hashSet.contains(3); // returns false (not found)
hashSet.add(2);
hashSet.contains(2); // returns true
hashSet.remove(2);
hashSet.contains(2); // returns false (already removed)

Note:

  • All values will be in the range of [1, 1000000].
  • The number of operations will be in the range of [1, 10000].
  • Please do not use the built-in HashSet library.

题目大意

动手实现一个hashset.不能用已经内置的函数。

解题方法

位图法

那么直接想到能不能真正模拟hashset呢?通过计算hash,或者一个元素一个坑的方法进行模拟?

参考了一下别人的代码,比我想的要机智一点。这个思路的方法是第一个维度只做hash,第二个维度保存具体元素。这个思想类似于HashMap中的bucket+链表桶。

我觉得这个方法最大的优点就是省内存,因为这种设计当需要的时候,才会产生第二个维度的数据。

代码如下:

class MyHashSet:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.buckets = 1000
self.itemsPerBucket = 1001
self.table = [[] for _ in range(self.buckets)] def hash(self, key):
return key % self.buckets def pos(self, key):
return key // self.buckets def add(self, key):
"""
:type key: int
:rtype: void
"""
hashkey = self.hash(key)
if not self.table[hashkey]:
self.table[hashkey] = [0] * self.itemsPerBucket
self.table[hashkey][self.pos(key)] = 1 def remove(self, key):
"""
:type key: int
:rtype: void
"""
hashkey = self.hash(key)
if self.table[hashkey]:
self.table[hashkey][self.pos(key)] = 0 def contains(self, key):
"""
Returns true if this set did not already contain the specified element
:type key: int
:rtype: bool
"""
hashkey = self.hash(key)
return (self.table[hashkey] != []) and (self.table[hashkey][self.pos(key)] == 1) # Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)

数组法

直接开辟这么大的一个数组,然后全部设置成False,哪里有数字哪里就是True。空间没有超。

class MyHashSet:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.set = [False] * 1000001 def add(self, key):
"""
:type key: int
:rtype: void
"""
self.set[key] = True def remove(self, key):
"""
:type key: int
:rtype: void
"""
self.set[key] = False def contains(self, key):
"""
Returns true if this set contains the specified element
:type key: int
:rtype: bool
"""
return self.set[key] # Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)

日期

2018 年 7 月 12 日 —— 天阴阴地潮潮,已经连着两天这样了
2018 年 11 月 17 日 —— 美妙的周末,美丽的天气

【LeetCode】705. Design HashSet 解题报告(Python)的更多相关文章

  1. LeetCode 705 Design HashSet 解题报告

    题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...

  2. LeetCode 706 Design HashMap 解题报告

    题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...

  3. LeetCode 705. Design HashSet (设计哈希集合)

    题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java ...

  4. 【LeetCode】706. Design HashMap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【Leetcode_easy】705. Design HashSet

    problem 705. Design HashSet 题意: solution1: class MyHashSet { public: /** Initialize your data struct ...

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. 『学了就忘』Linux文件系统管理 — 65、LVM逻辑卷管理介绍

    目录 1.LVM逻辑卷管理的简介 2.LVM逻辑卷管理的原理 3.总结建立LVM分区的步骤 1.LVM逻辑卷管理的简介 LVM是Logical Volume Manager的简称,中文就是逻辑卷管理. ...

  2. 日常Java 2021/9/26 (二柱升级版)

    package m; import java.util.Scanner;import java.util.Random; public class di_er { static int number= ...

  3. [PE结构]导出表结构浅析

    导出函数的总数-->以导出函数序号最大的减最小的+1,但导出函数序号是可自定义的,所以NumbersOfFunctions是不准确的 1.根据函数名称找,函数名称表->对应索引函数序号表中 ...

  4. nodejs代码初探之nodejs启动

    nodejs启动 入口在node_main.cc,解析参数后进入node.cc 中的node::Start() V8::Initialize() //初始化v8SetupProcessObject() ...

  5. 侵入式&非侵入式

    侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如:Struts框架 非侵入式设计 引入了框架,对现有的类结构没有影响. 例如:Hibernate框架 / Spring ...

  6. 【Linux】【Services】【Web】Haproxy

    1. 概念 1.1. 官方网站 http://www.haproxy.org/ 2. 安装 yum安装 yum -y install haproxy keepalived 配置haproxy日志,修改 ...

  7. 阿里云esc 登录时的相关提示

    1. 如果该ecs 未绑定密钥对,可以通过常规的用户名密码登录 2. 如果该 ecs 绑定了密钥对,则需要通过私钥进行登录 3. 如果使用 比如 securityCRT 登录时报 " A p ...

  8. jquery:iframe里面的元素怎样触发父窗口元素的事件?

    例如父窗口定义了一个事件. top: $(dom1).bind('topEvent', function(){}); 那么iframe里面的元素怎样触发父窗口dom1的事件呢?这样吗? $(dom1, ...

  9. spring的不同事务传播行为和用途。

    1.PROPAGATION_REQUIRED:如果当前没有事务,就创建一个事务,如果当前存在事务,就加入该事务,该设置是最常用的设置. 2.PROPAGATION_SUPPORTS:支持当前事务,如果 ...

  10. matplotlib画直线图的基本用法

    一  figure使用 1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 # 从-3到中取50个数 5 x = np.linspac ...