集合:唯一性,无序性;

基本结构:

 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

最新文章

  1. 存储构造题(Print Check)
  2. 对《[Unity官方实例教程 秘密行动] Unity官方教程《秘密行动》(十二) 角色移动》的一些笔记和个人补充,解决角色在地形上移动时穿透问题。
  3. (转)在Repeater中嵌套使用Repeater
  4. 使用WPF创建无边框窗体
  5. python 输出颜色的与样式的方法
  6. 实际开发---&gt;php时间函数
  7. Day4 《机器学习》第四章学习笔记
  8. 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
  9. Ubuntu 18.04 安装微信(Linux通用)
  10. HDU 1034(传递糖果 模拟)
  11. Android 异常 android.os.NetworkOnMainThreadException
  12. Session、Cookie、Cache、Token分别是什么及区别
  13. 解决《UNIX环境高级编程》(APUE)示例代码的编译问题
  14. 『TensorFlow』高级高维切片gather_nd
  15. 微信小程序----搜索框input回车搜索事件
  16. SSM整合(1): spring 与 springmvc 整合
  17. Asp.Net MVC Areas区域说明
  18. Docker系列之CentOS7安装Docker(一)
  19. snprintf()解析
  20. Jmeter实现webservice的接口测试

热门文章

  1. matlab之round any size rat isscalar ismatrix mean find max
  2. Dialog类介绍
  3. git pull --rebase
  4. VS(VisualStudio)中折叠代码、打开代码的快捷键
  5. excel中如何批量将所有的网址设为超链接
  6. SCOPE_IDENTITY的作用
  7. percona-xtrabackup备份mysql
  8. DP:Space Elevator(POJ 2392)
  9. 【USACO】namenum
  10. NEFU 2016省赛演练一 F题 (高精度加法)