作者: 负雪明烛
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利用ppp实现4G模块联网

    https://blog.csdn.net/qq361294382/article/details/52136126 https://blog.csdn.net/qq361294382/article ...

  2. 零基础学习java------day2------关键字、标志符、常量、进制键的转换、java中的数据类型、强制类型转换的格式

    今日内容要求: 1. 了解关键字的概念及特点,了解保留字 2. 熟练掌握标识符的含义,特点,可使用字符及注意事项 3. 了解常量的概念,进制,进制之间相互转换,了解有符号标识法的运算方式 4. 掌握变 ...

  3. ORACLE中dual用法详解

    基本上oracle引入dual为的就是符合语法1. 我们先从名称来说,dual不是缩写词,本身就是完整的单词.dual名词意思是对数,做形容词时是指二重的,二元的.2. Oracle中的dual表是一 ...

  4. oracle(创建数据库对象)

    1 --创建数据库 2 --1.SYSDBA系统权限 3 startup:--启动数据库. 4 shutdown:--关闭数据库. 5 alter database[mount]|[open]|[ba ...

  5. When do we pass arguments by reference or pointer?

    在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...

  6. LVS nat模型+dr模型

    nat模型 在 rs1 和 rs2  安装httpd  并配置测试页,启动 [root@rs1 ~]# yum install httpd -y[root@rs1 ~]# echo "thi ...

  7. Linux:expr、let、for、while、until、shift、if、case、break、continue、函数、select

    1.expr计算整数变量值 格式 :expr arg 例子:计算(2+3)×4的值 1.分步计算,即先计算2+3,再对其和乘4 s=`expr 2 + 3` expr $s \* 4 2.一步完成计算 ...

  8. maven打包插件详解

    maven-jar-plugin插件的使用及详解 该插件的xml配置及详解如下: <plugin> <groupId>org.apache.maven.plugins</ ...

  9. ClassLoader.loadClass()与Class.forName()的区别《 转》

    ClassLoader.loadClass()与Class.forName()区别: ClassLoader.loadClass()与Class.forName()大家都知道是反射用来构造类的方法,但 ...

  10. 关于tensorflow无法使用gpu

    python3.6 无法使用tensorflow gpu 环境名称 test1 在控制台里进入环境 conda activate test1 使用python python 查看gpu能否使用 pri ...