zoukankan      html  css  js  c++  java
  • 数据结构与算法之集合

        function Set(){
                this.items={};
            }
    
            Set.prototype={
                add:function(value){
                    if(!this.has(value)){
                        this.items[value] = value;
                        return true;
                    }
                    return false;
                },
                remove:function(value){
                    if(this.has(value)){
                        delete this.items[value];
                        return true;
                    }
                    return false;
    
                },
                has:function(value){
                    //return value in this.items;
                    return this.items.hasOwnProperty(value);
                },
                clear:function(){
                    this.items = {};
                },
                size:function(){
                    return Object.keys[this.items].length;
                    // var count = 0;
                    // for(var prop in this.items){
                    //     if(this.items.hasOwnProperty(prop)){
                    //         ++count;
                    //     }
                    // }
                    // return count;
                },
                values:function(){
                    return Object.keys(this.items);
                },
                //并集
                union:function(otherSet){
                    var unionSet  = new Set();
                    var values = this.values();
                    for(var i=0;i<values.length;i++){
                        unionSet.add(values[i]);
                    }
                    values = otherSet.values();
                    for(var i=0;i<values.length;i++){
                        unionSet.add(values[i]);
                    }
                    return unionSet;
                },
                //交集
                intersection:function(otherSet){
                    var intersectionSet = new Set();
                    var values = this.values();
                    for(var i=0;i<values.length;i++){
                        if(otherSet.has(values[i])){
                            intersectionSet.add(values[i]);
                        }
                    }
                    return intersectionSet;
                },
                //差集
                difference:function(otherSet){
                    var differenceSet = new Set();
                    var values = this.values();
                    for(var i=0;i<values.length;i++){
                        if(!otherSet.has(values[i])){
                            differenceSet.add(values[i]);
                        }
                    }
                    return differenceSet;
                },
                //子集
                subset:function(otherSet){
                    if(this.size()>otherSet.size()){
                        return false;
                    } else {
                        var values = this.values();
                        for(var i=0;i<values.length;i++){
                            if(!otherSet.has(values[i])){
                                return false;
                            }
                        }
                        return true;
                    }
    
                }
            };
  • 相关阅读:
    Android中的跨进程通信方法实例及特点分析(二):ContentProvider
    智能交通焕发勃勃生机,未来会呈现哪些巨变?
    VS2008下编译boost_1_47_0
    windows下用vs2008和boost结合编译程序
    查表法计算CRC16校验值
    MFC读写配置文件
    VS2008快捷键_大全
    关于VS2008中的targetver.h文件
    VC++ 实验室仿真虚拟仪器
    OLEDB简介
  • 原文地址:https://www.cnblogs.com/kerryw/p/8338257.html
Copyright © 2011-2022 走看看