zoukankan      html  css  js  c++  java
  • js:数据结构笔记8--集合

    集合:唯一性,无序性;

    基本结构:

     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

  • 相关阅读:
    Java核心技术 卷一 笔记四 库类的直接使用
    Java核心技术 卷一 笔记三 大数值及数组
    Java核心技术 卷一 笔记2 字符串的复制
    Java核心技术 卷一 笔记1
    修改css 样式后, hover事件 不生效
    修改 element ui input 输入框 样式不生效问题
    css3 计算属性
    Vue3 改动系列
    浏览器实现,向下滑动 鼠标滚轮,页面横向移动
    linux ceont0s7 vue 打包压缩图片 一直报错
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/4032022.html
Copyright © 2011-2022 走看看