js:数据结构笔记8--集合
2024-09-08 16:36:25
集合:唯一性,无序性;
基本结构:
function Set () {
this.dataStore = [];
this.add = add;
this.remove = remove;
this.contains =contains;
this.show = show;
}
function contains(data) {
var pos = this.dataStore.indexOf(data);
if(pos > -1) {
return true;
} else {
return false;
}
}
function add(data) {
var pos = this.dataStore.indexOf(data);
if( pos < 0) {
this.dataStore.push(data);
return true;
} else {
return false;
}
}
function remove(data) {
var pos = this.dataStore.indexOf(data);
if(pos > -1) {
this.dataStore.splice(pos,1);
return true;
} else {
return false;
}
}
function show() {
console.log(this.dataStore);
}
操作:demo
集合的基本操作:并集,交集,补集;
并集:
新增:
function union(set) {
var tempSet = new Set();
for(var i = 0; i < this.dataStore.length; ++i) {
tempSet.add(this.dataStore[i]);
}
for(var i = 0; i < set.dataStore.length; ++i) {
var setData = set.dataStore[i];
if(!tempSet.contains(setData)) {
tempSet.dataStore.push(setData);
}
}
return tempSet;
}
操作: demo;
交集:
新增:
function intersect(set) {
var tempSet = new Set();
for(var i = 0; i < this.dataStore.length; ++i) {
var thisData = this.dataStore[i];
if(set.contains(thisData)) {
tempSet.add(thisData);
}
}
return tempSet;
}
操作: demo;
补集:
新增: function difference(set) {
var tempSet = new Set();
for(var i = 0; i < this.dataStore.length; ++i) {
var thisData = this.dataStore[i];
if(!set.contains(thisData)) {
tempSet.add(thisData);
}
}
return tempSet;
}
父集判断:
新增:
function size() {
return this.dataStore.length;
}
function subset(set) {
if(this.size() > set.size()) {
return false;
} else {
for(var i = 0; i < this.dataStore.length; ++i) {
var thisData = this.dataStore[i];
if(!set.contains(thisData)) {
return false;
}
}
}
return true;
}
操作:demo
最新文章
- 存储构造题(Print Check)
- 对《[Unity官方实例教程 秘密行动] Unity官方教程《秘密行动》(十二) 角色移动》的一些笔记和个人补充,解决角色在地形上移动时穿透问题。
- (转)在Repeater中嵌套使用Repeater
- 使用WPF创建无边框窗体
- python 输出颜色的与样式的方法
- 实际开发--->;php时间函数
- Day4 《机器学习》第四章学习笔记
- eclipse svn插件卸载 重新安装 Subclipse卸载安装 The project was not built since its build path is incomplete This client is too old to work with the working copy at
- Ubuntu 18.04 安装微信(Linux通用)
- HDU 1034(传递糖果 模拟)
- Android 异常 android.os.NetworkOnMainThreadException
- Session、Cookie、Cache、Token分别是什么及区别
- 解决《UNIX环境高级编程》(APUE)示例代码的编译问题
- 『TensorFlow』高级高维切片gather_nd
- 微信小程序----搜索框input回车搜索事件
- SSM整合(1): spring 与 springmvc 整合
- Asp.Net MVC Areas区域说明
- Docker系列之CentOS7安装Docker(一)
- snprintf()解析
- Jmeter实现webservice的接口测试