705.设计哈希集合

2020-04-07

设计哈希集合

不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能

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

题解:

思路1: 使用数组设计哈希集合 (不建议)

之所以不建议是因为性能差 每次add,contains或者delete都要遍历看看这个值是不是存在

// 使用数组做hash
/**
* Initialize your data structure here.
*/
var MyHashSet = function () {
  this.hashContent = [];
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.add = function (key) {
  const data = this.hashContent;
  for (let i = 0; i < data.length; i++) {
    if (data[i] === key) return null;
  }
  data[data.length] = key;
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.remove = function (key) {
  const data = this.hashContent;
  for (let i = 0; i < data.length; i++) {
    if (data[i] === key) data.splice(i, 1);
  }
};

/**
 * Returns true if this set contains the specified element
 * @param {number} key
 * @return {boolean}
 */
MyHashSet.prototype.contains = function (key) {
  const data = this.hashContent;
  for (let i = 0; i < data.length; i++) {
    if (data[i] === key) return true;
  }
  return false;
};

/**
 * Your MyHashSet object will be instantiated and called as such:
 * var obj = new MyHashSet()
 * obj.add(key)
 * obj.remove(key)
 * var param_3 = obj.contains(key)
 */

2: 使用对象设计哈希集合

对象免去了遍历的烦恼 性能更高 代码更简单易读

/**
 * Initialize your data structure here.
 */
var MyHashSet = function () {
  this.hashContent = {};
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.add = function (key) {
  this.hashContent[key] = true;
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.remove = function (key) {
  this.hashContent[key] && delete this.hashContent[key];
};

/**
 * Returns true if this set contains the specified element
 * @param {number} key
 * @return {boolean}
 */
MyHashSet.prototype.contains = function (key) {
  if (this.hashContent[key]) return true;
  return false;
};

/**
 * Your MyHashSet object will be instantiated and called as such:
 * var obj = new MyHashSet()
 * obj.add(key)
 * obj.remove(key)
 * var param_3 = obj.contains(key)
 */

原文地址:https://www.cnblogs.com/lanpang9661/p/12651371.html

时间: 2024-07-31 22:23:13

705.设计哈希集合的相关文章

[Swift]LeetCode705. 设计哈希集合 | Design HashSet

Design a HashSet without using any built-in hash table libraries. To be specific, your design should include these functions: add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove

leetcode 705 设计哈希映射

一  题目概述 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. remove(value):将给定值从哈希集合中删除.如果哈希集合中没有这个值,什么也不做. 二 java算法实现 class MyHashSet { private Node[]arr=new Node[1024]; //2的指数,计算hash时可以用 & private class N

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

题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java Solution: Runtime: 58 ms, faster than 90.21% Memory Usage: 56.3 MB, less than 68.53% 完成日期:03/18/2019 关键点:boolean array class MyHashSet { boolean [] se

706.设计哈希映射

2020-04-08 设计哈希映射 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1. remove(key):如果映射中存在这个键,删除这个数值对. 题解: 思路1: 使用对象设计哈希集合 /** * Initialize your data structure here. */ va

第15章 hash_set哈希集合容器

/* 第15章 hash_set哈希集合容器   15.1 hash_set技术原理   15.2 hash_set应用基础   15.3 本章小结 略 */

C#泛型集合之——哈希集合

1.特点:HashSet 中元素不重复,容量为元素个数,自动增大.是一组值,是高性能的数学集合. 2.创建: (1)HashSet<类型> 集合名 = new HashSet<类型>(): //空集合 (2)HashSet<类型> 集合名 = new HashSet<类型>() { "马超", "关羽" }; (3)HashSet<类型> 集合名 = new HashSet<类型>(数组名);/

Hashed collections哈希集合

[定义] 有index的集合 [hash的原理] term for a situation when two different objects return the same hashcode: hash collision 就是无规律的一一对应排序,相同object对应的HASH应该相同,相同对应的HASH应该不同.具体实现:hashCode(It returns an int hash-code depending on the memory address of the object 根

LeetCode 706. Design HashMap (设计哈希映射)

题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. Java Solution: Runtime: 76 ms, faster than 27.53% Memory Usage: 58.2 MB, less than 31.57% 完成日期:03/18/2019 关键点:int array class MyHashMap { int [] map;

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表