木桶结构压缩数据
作者:互联网
//通过木桶结构压缩数据,getMidNum获取中间数 class Tong { constructor(){ this.data=[] this.length=0; } add(num){ if(!this.data[num]){ this.data[num]=1; }else{ this.data[num]++; } this.length++; } del(num){ if(this.data[num]){ this.data[num]--; this.length--; } } //获取中间数 getMidNum(){ let midlen=this.length>>1; let m=this.length%2; let midNum; for(let i=0;i<this.data.length;i++){ if(this.data[i]){ if(midlen-this.data[i]+m>0){ midlen=midlen-this.data[i] }else{ midNum=i; break; } } } return midNum; } } const t=new Tong() for(let i=0;i<100000;i++){ t.add(1) t.add(2) t.add(3) t.add(1) } console.log(t) console.log(t.getMidNum())
标签:midNum,压缩,木桶,midlen,length,num,let,data,结构 来源: https://www.cnblogs.com/caoke/p/12652692.html