1. 集合数据结构
集合是由一组无序且不重复的项组成,和数学中的有限集合概念一样,空集就是不包含任何元素的集合。
1.1 创建集合
- add(value):向集合添加一个新的项。
- delete(value):从集合中移除一个值。
- has(value):如果值在集合中,返回true,否则返回false。
- clear():移除集合中的所有项。
- size():返回集合所包含元素的数量。与数组的length属性类似。
values():返回一个包含集合中所有值的数组。
function Set() {let items = {};/*** 判断集合中是否存在给定元素** @param value* @returns {boolean}*/this.has = function (value) {return items.hasOwnProperty(value);};/*** 添加新的项** @param value* @returns {boolean}*/this.add = function (value) {if (!this.has(value)) {items[value] = value; // 同时作为键和值保存,方便查找return true;}return false;};/*** 移除元素** @param value* @returns {boolean}*/this.delete = function (value) {if (this.has(value)) {delete items[value];return true;}return false;};/*** 移除集合中所有项*/this.clear = function () {items = {};};/*** 返回集合所包含的元素数量** @returns {number}*/this.size = function () {return Object.keys(items).length;};/*** 返回一个包含集合中所有元素的数组** @returns {[]}*/this.values = function () {return Object.keys(items).map(key => items[key]);};/*** 求并集 A∪B** @param otherSet*/this.union = function (otherSet) {let unionSet = new Set();let values = this.values();values.forEach(value => unionSet.add(value));values = otherSet.values();values.forEach(value => unionSet.add(value));return unionSet;};/*** 求交集 A∩B** @param otherSet* @returns {Set}*/this.intersection = function (otherSet) {let intersectionSet = new Set();let values = this.values();values.forEach(value => {if (otherSet.has(value)) {intersectionSet.add(value);}});return intersectionSet;};/*** 求差集 A-B** @param otherSet* @returns {Set}*/this.difference = function (otherSet) {let differenceSet = new Set();let values = this.values();values.forEach(value => {if (!otherSet.has(value)) {differenceSet.add(value);}});return differenceSet;};/*** 判断A是否是B的子集** @param otherSet* @returns {boolean}*/this.subset = function (otherSet) {if (this.size() > otherSet.size()) {return false;} else {let values = this.values();for (let i = 0; i < values.length; i++) {if (!otherSet.has(values[i])) {return false;}}return true;}};}
2. 集合相关问题
2.1 设计一个 HashSet
```javascript // https://leetcode.com/problems/design-hashset/
// 不使用任何内建的哈希表库,设计一个 HashSet
// 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.
/**
- Initialize your data structure here. */ var MyHashSet = function() { this.items = {}; };
/**
- @param {number} key
- @return {void} */ MyHashSet.prototype.add = function(key) { if (!this.contains(key)) { this.items[key] = key; } };
/**
- @param {number} key
- @return {void} */ MyHashSet.prototype.remove = function(key) { if (this.contains(key)) { delete this.items[key]; } };
/**
- Returns true if this set contains the specified element
- @param {number} key
- @return {boolean} */ MyHashSet.prototype.contains = function(key) { return this.items.hasOwnProperty(key); };
/**
- 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) */ ```
